QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#414898 | #8057. Best Carry Player 4 | MHo2005 | WA | 1ms | 3780kb | C++14 | 3.2kb | 2024-05-20 00:39:31 | 2024-05-20 00:39:31 |
Judging History
answer
#include <bits/stdc++.h>
#include <random>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi> vpi;
#define mp make_pair
#define f first
#define s second
#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define rsz resize
#define bk back()
#define pb push_back
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define For(i,a) FOR(i,0,a)
#define ROF(i,a,b) for (int i = (b)-1; i >= (a); --i)
#define Rof(i,a) ROF(i,0,a)
#define trav(a,x) for (auto& a: x)
const int MOD = 1e9+7;
const ld PI = acos((ld)-1);
mt19937 rng; // or mt19937_64
template<class T> bool ckmin(T& a, const T& b) {
return b < a ? a = b, 1 : 0; }
void DBG() { cerr << "]" << endl; }
template<class H, class... T> void DBG(H h, T... t) {
cerr << h; if (sizeof...(t)) cerr << ", ";
DBG(t...); }
#ifdef LOCAL // compile with -DLOCAL
#define dbg(...) cerr << "LINE(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define dbg(...) 0
#endif
void solve() {
int m;cin>>m;
vi a(m),b(m);
ll sum1=0,sum2=0;
int larg1=0, larg2=0;
For(i,m)cin>>a[i], sum1+=a[i];
For(i,m)cin>>b[i], sum2+=b[i];
if(sum2>sum1) swap(a,b);
For(i,m) if(a[i]) larg1 = max(larg1,i);
For(i,m) if(b[i]) larg2 = max(larg2,i);
a[0] = MOD, b[0]=MOD;
vi c(m), d(m);
For(i,m) c[i] = a[i], d[i] = b[i];
// greedy pairs that sum to m-1
set<int> avail;
For(i,m)if(a[i])avail.insert(i);
bool carryable = false;
bool dones = false;
ll ans = 0;
For(i,m) if(b[i]) {
while(b[i]) {
auto it = avail.lower_bound(m-1-i);
if(it==avail.end()) {
if(!carryable && dones && i==larg2) carryable = true;
break;
}
dones = true;
int val = *it;
ll tmp = min(a[val], b[i]);
dbg(i,val,a[val], b[i], tmp);
b[i] -= tmp;
a[val] -= tmp;
ans += tmp;
if(i+val>m-1) carryable = true;
if(!a[val]) avail.erase(val);
}
}
if(carryable) {
dbg("A");
cout << ans << '\n';
} else {
avail.clear();
For(i,m) a[i]=c[i], b[i] = d[i];
For(i,m)if(a[i])avail.insert(i);
carryable = false;
dones = false;
ans = 0;
For(i,m) if(b[i]) {
auto it2 = avail.lower_bound(m-i);
if(it2 != avail.end()) {
int val2 = *it2;
if(b[i] && a[val2]) {
carryable = true;
ans++;
b[i]--;
a[val2]--;
if(!a[val2]) avail.erase(val2);
break;
}
}
}
For(i,m) if(b[i]) {
while(b[i]) {
auto it = avail.lower_bound(m-1-i);
if(it==avail.end()) {
if(!carryable && dones && i==larg2) carryable = true;
break;
}
dones = true;
int val = *it;
ll tmp = min(a[val], b[i]);
dbg(i,val,a[val], b[i], tmp);
b[i] -= tmp;
a[val] -= tmp;
ans += tmp;
if(i+val>m-1) carryable = true;
if(!a[val]) avail.erase(val);
}
}
cout << ans << '\n';
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int t;cin>>t;
For(i,t) solve();
}
/* stuff you should look for
* int overflow, array bounds
* special cases (n=1?)
* do smth instead of nothing and stay organized
* WRITE STUFF DOWN
*/
详细
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3780kb
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 28
result:
wrong answer 5th numbers differ - expected: '29', found: '28'