QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#117801#6683. Difficult Constructive Problemfeecle6418AC ✓17ms5848kbC++141.4kb2023-07-02 09:24:522023-07-02 09:24:53

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 09:24:53]
  • 评测
  • 测评结果:AC
  • 用时:17ms
  • 内存:5848kb
  • [2023-07-02 09:24:52]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 9, inf = 1e9;
int n, m, fmn[100005][2], fmx[100005][2], b[100005];
char a[100005];
string Solver() {
	for (int i = 1; i <= n; i++) b[i] = a[i] - '0';
	fmn[n][b[n]] = 0;
	fmx[n][b[n]] = 0;
	fmn[n][1 - b[n]] = inf;
	fmx[n][1 - b[n]] = -inf;
	for (int i = n - 1; i >= 1; i--) {
		for (int j = 0; j < 2; j++) {
			fmn[i][j] = inf;
			fmx[i][j] = -inf;
			if (b[i] != j && a[i] != '?') continue;
			for (int k = 0; k < 2; k++) {
				fmn[i][j] = min(fmn[i][j], fmn[i + 1][k] + (j != k));
				fmx[i][j] = max(fmx[i][j], fmx[i + 1][k] + (j != k));
			}
		}
	}
	int u = m;
	string str;
	str.resize(n);
	for (int i = 1; i <= n; i++) {
		bool ok = 0;
		for (int j = 0; j < 2; j++) {
			int nu = u;
			if (i > 1) nu -= (j != str[i - 2] - '0');
			if (fmn[i][j] <= nu && nu <= fmx[i][j] && fmn[i][j] % 2 == nu % 2) {
				str[i - 1] = j + '0';
				ok = 1;
				u = nu;
				break;
			}
		}
		if (!ok) {
			str.resize(n + 5);
			fill(str.begin(), str.end(), '2');
			return str;
		}
	}
	return str;
}
void Solve() {
	scanf("%d%d%s", &n, &m, a + 1);
	string ans;
	if (a[n] == '?') {
		a[n] = '1', ans = Solver();
		a[n] = '0', ans = min(ans, Solver());
	} else ans = Solver();
	if (ans.length() > n) cout << "Impossible\n";
	else cout << ans << '\n';
}
int main() {
	int t;
	scanf("%d", &t);
	while (t--) Solve();
}

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 17ms
memory: 5848kb

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