QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#178295#6683. Difficult Constructive Problemucup-team004#AC ✓6ms4112kbC++202.0kb2023-09-13 20:47:372023-09-13 20:47:37

Judging History

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

  • [2023-09-13 20:47:37]
  • 评测
  • 测评结果:AC
  • 用时:6ms
  • 内存:4112kb
  • [2023-09-13 20:47:37]
  • 提交

answer

#include <bits/stdc++.h>

using i64 = long long;

void solve() {
    int n, k;
    std::cin >> n >> k;
    
    std::string s;
    std::cin >> s;
    
    if (s == std::string(n, '?')) {
        s[0] = '0';
        for (int i = 1; i < n; i++) {
            if (n - i <= k) {
                s[i] = s[i - 1] ^ 1;
            } else {
                s[i] = s[i - 1];
            }
        }
        std::cout << s << "\n";
        return;
    }
    
    int l = 0, r = 0;
    int parity = 0;
    std::vector<int> p{-1};
    for (int i = 0; i < n; i++) {
        if (s[i] != '?') {
            p.push_back(i);
        }
    }
    p.push_back(n);
    
    auto add = [&](int x, int y, int t = 1) {
        int len = y - x - 1;
        if (x == -1 || y == n) {
            r += len * t;
            if (len > 0) {
                parity += t;
            }
        } else {
            l += (s[x] != s[y]) * t;
            int mx = len + 1;
            if (mx % 2 != (s[x] != s[y])) {
                mx--;
            }
            r += mx * t;
        }
    };
    
    auto check = [&]() {
        return l <= k && k <= r && (parity > 0 || (k - l) % 2 == 0);
    };
    
    for (int i = 0; i + 1 < p.size(); i++) {
        add(p[i], p[i + 1]);
    }
    
    if (!check()) {
        std::cout << "Impossible\n";
        return;
    }
    
    for (int i = 0; i + 1 < p.size(); i++) {
        for (int j = p[i] + 1; j < p[i + 1]; j++) {
            add(j - 1, p[i + 1], -1);
            s[j] = '0';
            add(j - 1, j);
            add(j, p[i + 1]);
            if (!check()) {
                add(j - 1, j, -1);
                add(j, p[i + 1], -1);
                s[j] = '1';
                add(j - 1, j);
                add(j, p[i + 1]);
            }
        }
    }
    
    std::cout << s << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    
    int t;
    std::cin >> t;
    
    while (t--) {
        solve();
    }
    
    return 0;
}

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

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3884kb

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: 6ms
memory: 4112kb

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