QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#450458 | #1289. A + B Problem | cxm1024 | TL | 4ms | 36864kb | C++14 | 2.7kb | 2024-06-22 13:38:19 | 2024-06-22 13:38:19 |
Judging History
answer
#include <bits/stdc++.h>
#define endl "\t--- in " << __LINE__ << endl
using namespace std;
int nn[1000010], mm[1000010];
string ss[1000010];
vector<int> calc(int n, int m, string s, int ed) {
int cnt = 0;
for (int i = 0; i <= ed; i++)
cnt += s[i] == '0';
vector<int> a, b;
int sum = m - cnt - (s[ed] == '1');
for (int i = 0; i < ed; i++) {
if (s[i] == '0' || sum) b.push_back(s[i] - '0'), sum -= s[i] - '0';
else a.push_back(s[i] - '0');
}
b.push_back(s[ed] - '0');
for (int i = ed + 1; i < s.size(); i++)
a.push_back(s[i] - '0');
vector<int> c(max(a.size(), b.size()) + 1);
reverse(a.begin(), a.end()), reverse(b.begin(), b.end());
a.resize(c.size()), b.resize(c.size());
int lst = 0;
for (int i = 0; i < c.size(); i++)
c[i] = (a[i] + b[i] + lst) & 1, lst = (a[i] + b[i] + lst) >> 1;
while (c.size() > 1 && c.back() == 0) c.pop_back();
reverse(c.begin(), c.end());
return c;
}
void Solve(int n, int m, string s) {
if (n < m) swap(n, m);
vector<int> res;
int cnt = 0;
for (int i = 0; i < m; i++)
cnt += s[i] == '0';
for (int ed = m - 1; ed < s.size() && cnt + s[ed] - '0' <= m; cnt += (s[++ed] == '0')) {
vector<int> c = calc(n, m, s, ed);
if (c.size() > res.size() || (c.size() == res.size() && c > res))
res = c;
}
for (int x : res)
cout << x;
cout << "\n";
}
void Solve2(int n, int m, string s) {
if (n < m) swap(n, m);
int l = 0, r = 0;
for (int i = 0; i < m; i++)
l += s[i] == '0';
for (int i = 0; i < s.size(); i++)
r += s[i] == '0';
r = min(r, m);
while (l + 30 < r) {
int lmid = (l * 2 + r) / 3, rmid = (l + r * 2) / 3;
int now = 0;
for (int i = 0; i < s.size(); i++) {
now += s[i] == '0';
if (now == lmid) {lmid = i; break;}
}
now = 0;
for (int i = 0; i < s.size(); i++) {
now += s[i] == '0';
if (now == rmid) {rmid = i; break;}
}
auto cl = calc(n, m, s, max(m - 1, lmid)), cr = calc(n, m, s, max(m - 1, rmid));
if (cl.size() > cr.size() || (cl.size() == cr.size() && cl > cr))
r = rmid;
else l = lmid;
}
vector<int> res;
for (int i = l; i <= r; i++) {
int now = 0, x;
for (int j = 0; j < s.size(); j++) {
now += s[j] == '0';
if (now == i) {x = j; break;}
}
auto c = calc(n, m, s, max(m - 1, x));
if (c.size() > res.size() || (c.size() == res.size() && c > res))
res = c;
}
for (int x : res)
cout << x;
cout << "\n";
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int T, sum = 0;
cin >> T;
for (int i = 1; i <= T; i++) {
cin >> nn[i] >> mm[i] >> ss[i];
sum += nn[i] + mm[i];
}
if (sum <= 20000) {
for (int i = 1; i <= T; i++)
Solve(nn[i], mm[i], ss[i]);
}
else {
for (int i = 1; i <= T; i++)
Solve2(nn[i], mm[i], ss[i]);
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 36864kb
input:
3 4 3 1000101 2 2 1111 1 1 00
output:
1101 110 0
result:
ok 3 lines
Test #2:
score: -100
Time Limit Exceeded
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...