QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#185137 | #6683. Difficult Constructive Problem | jeffqi# | AC ✓ | 11ms | 4848kb | C++20 | 2.3kb | 2023-09-21 17:50:01 | 2023-09-21 17:50:01 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define vi vector<int>
#define vll vector<ll>
#define eb emplace_back
#define pb push_back
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
#define sz(v) ((int)v.size())
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define umap unordered_map
#define uset unordered_set
#define mset multiset
#define ui unsigned int
#define ull unsigned ll
#define i128 __int128
using namespace std;
namespace qiqi {
void fail() {
cout << "Impossible\n";
return;
}
void main() {
int n,m; string s;
cin >> n >> m >> s;
vi mn(n+1),mx(n+1),nxt(n);
auto cr = [&](const string &s,int i,int j) {
if (j == n) {
return pair(0,j-i-1);
}
else {
int len = j-i;
if (s[i] != s[j]) {
return pair(1,len+len%2-1);
}
else {
return pair(0,len-len%2);
}
}
};
for (int i = n-1,lst = n; i >= 0; i--) {
nxt[i] = lst;
if (s[i] != '?') {
auto [pmn,pmx] = cr(s,i,lst);
mn[i] = mn[lst]+pmn;
mx[i] = mx[lst]+pmx;
lst = i;
}
}
auto calc = [&](string s) {
int c = 0;
for (int i = 1; i < n; i++) {
if (s[i] != '?') {
c += s[i] != s[i-1];
}
else {
int p = nxt[i];
bool flg = 0;
for (char ch = '0'; ch < '1'+1; ch++) {
s[i] = ch;
int nc = c+(ch != s[i-1]);
auto [pmn,pmx] = cr(s,i,p);
if (nc+pmn+mn[p] <= m && nc+pmx+mx[p] >= m) {
flg = 1; c = nc; break;
}
}
if (!flg) {
return string();
}
}
}
if (c == m) {
return s;
}
return string();
};
if (s[0] != '?') {
auto t = calc(s);
if (!t.empty()) {
cout << t << '\n';
return;
}
return fail();
}
else {
s[0] = '0';
auto t = calc(s);
if (!t.empty()) {
cout << t << '\n';
return;
}
s[0] = '1';
t = calc(s);
if (!t.empty()) {
cout << t << '\n';
return;
}
return fail();
}
}
}
int main() {
// clock_t st = clock();
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T = 1;
cin >> T;
while (T--) {
qiqi::main();
}
// cout << (double)(clock()-st)/CLOCKS_PER_SEC;
return 0;
}
这程序好像有点Bug,我给组数据试试?
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3616kb
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: 11ms
memory: 4848kb
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