QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#339314 | #8057. Best Carry Player 4 | asxziill | WA | 61ms | 3540kb | C++23 | 2.0kb | 2024-02-27 01:22:24 | 2024-02-27 01:22:25 |
Judging History
answer
#include <bits/stdc++.h>
using ll = long long;
/*
我们先假设有一个进位,则我们只需找到尽可能多的数对 >= m - 1
然后再讨论进位的来源,如果本来的数对有>= m的则将他放到最后面即可
如果没有,再查看未匹配的数 最大的数比已经匹配的最小的数大,如果大, 则可以将其替换
如果还没有,最后检查是否能拆掉一个数对满足进位(即存在数>= m即可
匹配数对要由大到小,先将较小的数拿掉,这样会在不影响答案的情况下更容易凑出大于m的
*/
void solve() {
ll m;
std::cin >> m;
std::vector<ll> a(m), b(m);
for (int i = 0; i < m; i++) {
std::cin >> a[i];
}
for (int i = 0; i < m; i++) {
std::cin >> b[i];
}
//补0
if (std::accumulate(a.begin(), a.end(), 0ll) > std::accumulate(b.begin(), b.end(), 0ll)) {
std::swap(a, b);
}
a[0] += std::accumulate(b.begin(), b.end(), 0ll) - std::accumulate(a.begin(), a.end(), 0ll);
std::map<ll, ll> cnt;
for (int i = 0; i < m; i++) {
if (b[i]) {
cnt[i] = b[i];
}
}
bool carry = false;
bool up = false;
for (int i = 0; i < m; i++) {
if (cnt.lower_bound(m - i) != cnt.end()) {
up = true;
break;
}
}
ll mn = m;
ll ans = 0;
for (int i = m - 1; i >= 0; i--) {
auto it = cnt.lower_bound(m - 1 - i);
while (it != cnt.end() && a[i]) {
ll d = std::min(a[i], it->second);
a[i] -= d;
it->second -= d;
ans += d;
mn = std::min(mn, it->first);
if (i + it->first >= m) {
carry = true;
}
if (it->second == 0) {
it = cnt.erase(it);
}
}
}
if (carry) {
;
}
else if (!cnt.empty() && cnt.rbegin()->first > mn) {
;
}
else if (up) {
ans -= 1;
}
else {
ans = 0;
}
std::cout << ans << "\n";
}
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3536kb
input:
5 2 1 2 3 4 3 1 0 1 0 1 0 4 1 0 0 1 1 1 1 1 5 123456 114514 1919810 233333 234567 20050815 998244353 0 0 0 10 5 3 5 3 2 4 2 4 1 5 9 9 8 2 4 4 3 5 3 0
output:
5 1 2 467900 29
result:
ok 5 number(s): "5 1 2 467900 29"
Test #2:
score: -100
Wrong Answer
time: 61ms
memory: 3540kb
input:
100000 5 0 1 1 1 1 0 0 1 0 0 5 0 0 0 0 0 1 1 1 0 0 5 0 0 2 1 1 0 2 1 0 1 5 0 0 0 0 0 1 2 1 0 0 5 0 1 0 1 1 0 0 1 1 1 5 2 0 0 0 1 1 0 0 0 3 5 2 0 0 1 1 0 2 1 1 1 5 0 0 0 0 2 0 0 0 0 1 5 0 0 0 0 0 0 1 1 0 0 5 4 0 0 0 0 0 0 0 1 0 5 0 0 0 0 1 2 1 1 0 0 5 0 2 3 0 0 0 0 0 1 0 5 1 1 1 0 1 1 0 1 0 1 5 0 0 0...
output:
2 -1 4 -1 3 3 3 2 -1 0 1 1 3 0 3 0 0 -1 -1 0 0 0 4 0 4 1 -1 2 3 3 1 5 0 -1 2 0 0 1 1 -1 0 3 5 3 2 2 2 -1 1 2 2 2 -1 3 0 2 1 1 0 1 -1 4 1 1 2 2 -1 3 3 -1 2 -1 1 -1 0 1 1 2 0 3 4 -1 2 5 -1 2 1 0 0 -1 3 2 3 -1 2 0 4 3 3 0 2 2 1 1 3 1 1 -1 0 -1 1 -1 3 2 2 -1 2 1 1 0 1 0 0 2 4 1 3 3 2 2 2 -1 2 -1 0 2 3 1...
result:
wrong answer 2nd numbers differ - expected: '0', found: '-1'