QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#125199#6683. Difficult Constructive ProblemHOLICAC ✓19ms3656kbC++143.7kb2023-07-16 05:18:062023-07-16 05:18:07

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-16 05:18:07]
  • 评测
  • 测评结果:AC
  • 用时:19ms
  • 内存:3656kb
  • [2023-07-16 05:18:06]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 1009;
const int M = 5e5 + 1009;
int n, k, o;
char s[N];
struct node{
    int p, f, len, e;
};
string solve(){
    k = o;
    for(int i = 1; i < n; i ++) {
        if(s[i] == '?' || s[i + 1] == '?') continue;
        if(s[i] != s[i + 1]) k --;
    }
    int minn = 0, maxn = 0;
    for(int i = 1; i < n; i ++) {
        if(s[i] != '?' && s[i + 1] == '?') {
            int len = 0;
            while(i + len + 1 <= n && s[i + len + 1] == '?') len ++;
            if(s[i] - '0' != s[i + len + 1] - '0') {
                minn ++;
                maxn += (len / 2) * 2 + 1;
            } else maxn += (len + 1) / 2 * 2;
            i += len;
        }
    }
    if(k < minn || ((k & 1) != (minn & 1)) || k > maxn) return "2";
    string S = "";
    for(int i = 1; i <= n; i ++) {
        // cout << i << endl;
        if(s[i] != '?') S += s[i];
        else {
            int len = 1;
            while(s[i + len] == '?') len ++;
            //cout << len << endl;
            int f = s[i - 1] - '0', e = s[i + len] - '0';
            if(f != e) {
                minn --;
                maxn -= (len / 2) * 2 + 1;
            } else maxn -= (len + 1) / 2 * 2;
            int Minn = max(0, k - maxn), Maxn = k - minn;
            if(f != e) {
                Minn --; Maxn --;
                Minn = (Minn + 1) / 2; Maxn /= 2;
                if(f == 0) {
                    int t = len - (Minn * 2);
                    k --;
                    while(t --) S += "0";
                    while(Minn --) {
                        S += "10";
                        k -= 2;
                    }
                } else {
                    int t = len - (Minn * 2);
                    while(t --) S += "0";
                    k --;
                    while(Minn --) {
                        S += "01";
                        k -= 2;
                    }
                }
            } else if(f == e) {
                Minn = (Minn + 1) / 2; Maxn /= 2;
                if(f == 0) {
                    int t = len - max(0, (Minn * 2) - 1);
                    while(t --) S += "0";
                    if(Minn) {
                        S += "1";
                        Minn --;
                        k -= 2;
                    }
                    while(Minn --) {
                        S += "01";
                        k -= 2;
                    }
                } else {
                    if(Maxn == 0) {
                        int t = len;
                        while(t --) S += "1";
                    } else {
                        if(Minn) Minn --;
                        int t = len - (Minn * 2);
                        while(t --) S += "0"; k -= 2;
                        while(Minn --) S += "10", k -= 2;
                    }
                }
            }
            i += len - 1;
        }
    }
    return S;
}
void work() {
    cin >> n >> k;
    cin >> s + 1;
    int flag1 = (s[1] == '?'), flag2 = (s[n] == '?');
    o = k;
    string ans = "2";
    for(int j = 0; j <= 1; j ++)
        if(flag1 || s[1] == '0' + j) {
            s[1] = '0' + j;
            for(int k = 0; k <= 1; k ++)
                if(flag2 || s[n] == '0' + k) {
                    s[n] = '0' + k;
                  //   cout << s + 1 << endl;
                    ans = min(ans, solve());
                }
        }
    if(ans == "2") cout << "Impossible" << endl;
    else cout << ans << endl;
}
signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int Case = 1;
    cin >> Case;
    while(Case --) work();
    return 0;
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 19ms
memory: 3656kb

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