QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#712021 | #8057. Best Carry Player 4 | acwing_gza# | RE | 2ms | 9756kb | C++20 | 1.7kb | 2024-11-05 14:19:03 | 2024-11-05 14:19:03 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef ll i64;
typedef __int128 i128;
constexpr int maxn=5e5+9;
int m;
i64 a[maxn],b[maxn];
i64 ta[maxn],tb[maxn];
ll suma,sumb;
void solve()
{
cin>>m;
int n=m; suma=sumb=0;
for(int i=0;i<m;++i) cin>>a[i],suma+=ta[i]=a[i];
for(int i=0;i<m;++i) cin>>b[i],sumb+=tb[i]=b[i];
int maxa,maxb;
// printf("suma=%lld sumb=%lld\n",suma,sumb);
if(suma>sumb)
{
for(int i=0;i<m;++i) swap(a[i],b[i]);
swap(suma,sumb);
}
a[0]+=sumb-suma;
for(int i=m-1;i>=0;--i)
if(a[i])
{
maxa=i;
break;
}
for(int i=m-1;i>=0;--i)
if(b[i])
{
maxb=i;
break;
}
int ptr=m-1;
ll ans=0;
int flag=0,flag2=0;
ll ss=0;
for(int i=0;i<m;++i)
{
ss+=b[m-i];
if(ss && a[i]) { flag2=1; break; }
}
for(int i=0;i<m;++i)
{
if(!a[i]) continue;
while(ptr>=0 && !b[ptr]) --ptr;
if(ptr<0) break;
if(i+ptr>=m) flag=1;
while(ptr>=m-1-i && a[i])
{
int match=std::min(a[i],b[ptr]);
//printf("match %d of %d in a and %d in b\n",match,i,ptr);
ans+=match;
b[ptr]-=match;
a[i]-=match;
if(!b[ptr]) --ptr;
}
if(ptr<0) break;
}
for(int i=m-maxb;i<m;++i)
if(a[i]) flag=1;
for(int i=m-maxa;i<m;++i)
if(b[i]) flag=1;
cout<<(flag?ans:(flag2==0?0:ans-1))<<endl;
}
int main()
{
// ios::sync_with_stdio(0);
// cin.tie(0); cout.tie(0);
int t=1;
cin>>t;
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 9756kb
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
Runtime Error
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 0 4 0 3 3 3 2 0 0 1 1 3 0 3 0 0 0 0 0 0 0 4 0 4 1 0 2 3 3 1 5 0 0 2 0 0 1 1 0 0 3 5 3 2 2 2 0 1 2 2 2 0 3 0 2 1 1 0 1 0 4 0 0 2 2 0 3 3 0 2 0 1 0 0 1 1 2 0 3 4 0 2 5 0 2 1 0 0 0 3 2 3 0 2 0 4 3 3 0 2 2 0 1 3 1 1 0 0 0 1 0 3 2 2 0 2 1 1 0 1 0 0 2 4 1 3 3 2 2 2 0 2 0 0 2 3 1 3 1 0 2 2 3 0 1 2 0 1 1 ...