QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#450517#1289. A + B ProblemAnonymelyAC ✓51ms6412kbC++141.8kb2024-06-22 14:50:042024-06-22 14:50:05

Judging History

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

  • [2024-06-22 14:50:05]
  • 评测
  • 测评结果:AC
  • 用时:51ms
  • 内存:6412kb
  • [2024-06-22 14:50:04]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define QwQ01AwA return 0

void solve() {
	int n, m;
	cin >> n >> m;
	if (n < m) swap(n, m);
	string s;
	cin >> s;
	vector <int> ans1, ans2;
	vector <int> pos(n + m + 1, -1);
	vector <int> sum(n + m, 0);
	int cnt = 0;
	for (int i = 0; i < n + m; i++) {
		if (i) sum[i] = sum[i - 1];
		if (s[i] == '0') sum[i]++;
		else cnt++, pos[cnt] = i;
	}
	for (int i = 0, j = 0; i < n + m; i++) {
		if (ans1.size() == n) ans2.push_back(s[i] - '0');
		else if (ans2.size() == m) ans1.push_back(s[i] - '0');
		else if (s[i] == '0') ans2.push_back(0);
		else {
			j++;
			int d = ans2.size() + n - m - ans1.size() + 1;
			if (j + d > cnt) {
				ans1.push_back(1);
				continue;
			}
			int r = pos[j + d];
			int dis = sum[r] - sum[i];
			if (dis > m - ans2.size() - 1) {
				ans1.push_back(1);
				continue;
			}
			ans2.push_back(1);
		}
	}
	// print
	reverse(ans1.begin(), ans1.end());
	reverse(ans2.begin(), ans2.end());
	while (!ans1.empty() && ans1.back() == 0) ans1.pop_back();
	while (!ans2.empty() && ans2.back() == 0) ans2.pop_back();
	if (ans1.size() < ans2.size()) swap(ans1, ans2);
	if (ans1.empty()) {
		cout << 0 << '\n';
		return ;
	}
	int add = 0;
	for (int i = 0; i < (int)ans2.size(); i++) {
		ans1[i] += ans2[i] + add;
		if (ans1[i] >= 2) ans1[i] -= 2, add = 1;
		else add = 0;
	}
	for (int i = ans2.size(); i < (int)ans1.size(); i++) {
		ans1[i] += add;
		if (ans1[i] >= 2) ans1[i] -= 2, add = 1;
		else add = 0;
	}
	if (add) cout << 1;
	for (int i = (int)ans1.size() - 1; i >= 0; i--) cout << ans1[i];
	cout << '\n';
}

void file() {
	freopen("partition.in", "r", stdin);
	freopen("partition.out", "w", stdout);
}

signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	int t;
	cin >> t;
	while (t--) solve();
	QwQ01AwA;
}

詳細信息

Test #1:

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

input:

3
4 3
1000101
2 2
1111
1 1
00

output:

1101
110
0

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 51ms
memory: 6412kb

input:

11110
10 8
111011010011100100
3 5
01011000
7 6
1110101010000
9 1
0110100101
1 9
0100001110
8 10
000101101011111000
9 6
011111111000111
1 9
1011101101
10 7
00100011000100000
4 9
1000101101010
8 4
100100110000
8 9
00101111011000101
8 9
11000000101011110
7 6
1111010100110
2 9
01001110101
4 5
100010100
...

output:

10011010100
11100
10101000
110100101
100001110
10000001100
1000010111
111101101
1110100000
111101010
11110000
1000011101
1001011110
10101110
101110101
11100
1111010
1000010
1011100010
10010101001
10010001
1001010
1000000010
1110
111
1111110001
10110111
1100010101
10000000
111000011
110
11111
1100101...

result:

ok 11110 lines