QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#277418#6683. Difficult Constructive Problemucup-team191#AC ✓8ms4668kbC++142.1kb2023-12-06 18:42:252023-12-06 18:42:25

Judging History

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

  • [2023-12-06 18:42:25]
  • 评测
  • 测评结果:AC
  • 用时:8ms
  • 内存:4668kb
  • [2023-12-06 18:42:25]
  • 提交

answer

#include <cstdio>
#include <iostream>

using namespace std;

const int N = 2e5 + 500;

string s, old_s, s_max, s_min, ans;
int n, k, dp_max[N], dp_min[N];


void precompute(){
   s_max = s; s_min = s;
   dp_max[n - 1] = 0; dp_min[n - 1] = 0;
   for(int i = n - 2;i >= 0;i--) {
        if(s[i] == '?') {
            s_max[i] = '0' + ('1' - s_max[i + 1]);
            s_min[i] = s_min[i + 1];
        }
        dp_max[i] = dp_max[i + 1] + (s_max[i] != s_max[i + 1]);
        dp_min[i] = dp_min[i + 1] + (s_min[i] != s_min[i + 1]);
    }
}

bool try_greedy(){
    precompute();
    if(dp_max[0] < k || dp_min[0] > k) return false;
    ans = s;
    int kk = k;
    for(int i = 1;i + 1 < n;i++) {
        if(s[i] != '?') { kk -= ans[i] != ans[i - 1]; continue; }
        if((ans[i - 1] != '0') + (s_max[i + 1] != '0') + dp_max[i + 1] >= kk &&
            (ans[i - 1] != '0') + (s_min[i + 1] != '0') + dp_min[i + 1] <= kk) {
            ans[i] = '0';
        } else {
            ans[i] = '1';
        }
        kk -= ans[i] != ans[i - 1];
    }
    return true;
}

void solve(){
    cin >> n >> k;
    cin >> old_s;
    s = old_s;
    if((s[0] != '1') && s[n - 1] != '1' && k % 2 == 0) {
        s[0] = '0'; s[n - 1] = '0';
        if(try_greedy()) {
            cout << ans << '\n';
            return;
        }
        s = old_s;
    }

    if((s[0] != '1') && s[n - 1] != '0' && k % 2 == 1) {
        s[0] = '0'; s[n - 1] = '1';
        if(try_greedy()) {
            cout << ans << '\n';
            return;
        }
        s = old_s;
    }

    if((s[0] != '0') && s[n - 1] != '1' && k % 2 == 1) {
        s[0] = '1'; s[n - 1] = '0';
        if(try_greedy()) {
            cout << ans << '\n';
            return;
        }
        s = old_s;
    }

    if((s[0] != '0') && s[n - 1] != '0' && k % 2 == 0) {
        s[0] = '1'; s[n - 1] = '1';
        if(try_greedy()) {
            cout << ans << '\n';
            return;
        }
        s = old_s;
    }

    cout << "Impossible\n";
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int T; cin >> T;
    for(;T--;) solve();
}


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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 8ms
memory: 4668kb

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