QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#117892#6683. Difficult Constructive Problemhos_lyricAC ✓20ms4752kbC++143.4kb2023-07-02 12:56:592023-07-02 12:57:08

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-02 12:57:08]
  • 评测
  • 测评结果:AC
  • 用时:20ms
  • 内存:4752kb
  • [2023-07-02 12:56:59]
  • 提交

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }


int N, K;
char S[100'010];

int main() {
  for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
    scanf("%d%d", &N, &K);
    scanf("%s", S);
    
    string ans = "Impossible";
    if (N == 1) {
      ans = S;
      if (ans[0] == '?') ans[0] = '0';
    } else {
      for (const char c0 : {'0', '1'}) if (S[0] == '?' || S[0] == c0)
      for (const char c1 : {'0', '1'}) if (S[N - 1] == '?' || S[N - 1] == c1)
      {
        if ((K & 1) == ((c0 != c1) ? 1 : 0)) {
          string s = S;
#define S do_not_use_S
          s[0] = c0;
          s[N - 1] = c1;
          vector<int> to(N, -1);
          for (int i = N - 1; --i >= 0; ) {
            to[i] = (s[i + 1] != '?') ? (i + 1) : to[i + 1];
          }
          vector<int> fs(N, 0), gs(N, 0);
          for (int i = N - 1; --i >= 0; ) if (s[i] != '?') {
            const int j = to[i];
            if (s[i] == s[j]) {
              fs[i] = fs[j] + 0;
              gs[i] = gs[j] + 2 * ((j - i) / 2);
            } else {
              fs[i] = fs[j] + 1;
              gs[i] = gs[j] + 1 + 2 * ((j - i - 1) / 2);
            }
          }
// cerr<<s<<" "<<to<<" "<<fs<<" "<<gs<<endl;
          if (fs[0] <= K && K <= gs[0]) {
            int now = 0;
            for (int i = 1; i < N - 1; ++i) {
              const int j = to[i];
              for (const char c : {'0', '1'}) if (s[i] == '?' || s[i] == c) {
                int tmp = now;
                if (s[i - 1] != c) ++tmp;
                int f = tmp + fs[j];
                int g = tmp + gs[j];
                if (c == s[j]) {
                  f += 0;
                  g += 2 * ((j - i) / 2);
                } else {
                  f += 1;
                  g += 1 + 2 * ((j - i - 1) / 2);
                }
// cerr<<i<<" "<<c<<": ["<<f<<", "<<g<<"]"<<endl;
                if (f <= K && K <= g) {
                  s[i] = c;
                  now = tmp;
                  goto found;
                }
              }
              assert(false);
             found:{}
            }
            chmin(ans, s);
          }
        }
      }
    }
    puts(ans.c_str());
  }
#ifndef LOCAL
  break;
#endif
  }
  return 0;
}

这程序好像有点Bug,我给组数据试试?

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 3576kb

input:

5
9 6
1?010??01
9 5
1?010??01
9 6
100101101
9 5
100101101
9 3
????????1

output:

100100101
Impossible
100101101
Impossible
000000101

result:

ok 5 lines

Test #2:

score: 0
Accepted
time: 20ms
memory: 4752kb

input:

6110
2 0
01
9 5
????110??
18 8
???111???00??010??
11 8
001??01???0
2 0
00
8 1
?????111
11 2
110???01???
13 7
??100???01??1
6 2
?????0
18 8
110??11??011??110?
12 5
00??011??00?
20 10
??01???0011???0010??
1 0
?
13 6
???10??110??0
6 2
00???1
17 10
??010??001???11??
5 2
????0
14 7
010???00???11?
2 1
01
...

output:

Impossible
001011001
000111000000101010
00101010010
00
00000111
11000001111
0010000101001
000010
110001100011001101
000001101001
00010000011001001010
0
0001000110010
Impossible
00010010010101100
00010
01000100010111
01
0100100001
Impossible
001100010100100101
00111111000
000
01111001
001000000100010...

result:

ok 6110 lines