QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#637353 | #8057. Best Carry Player 4 | ucup-team073# | TL | 5ms | 15400kb | C++20 | 1.7kb | 2024-10-13 12:18:51 | 2024-10-13 12:18:52 |
Judging History
answer
#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read(){
int x=0,f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar())f^=ch=='-';
for(;isdigit(ch);ch=getchar())x=x*10+(ch^48);
return f?x:-x;
}
inline void chmin(int &x,int y){x=min(x,y);}
inline void chmax(int &x,int y){x=max(x,y);}
const int N=5e5+5,inf=1e15;
int n,sa,sb,A[N],B[N],oriA[N],oriB[N];
void solve(){
n=read();
for(int i=0;i<n;++i)sa+=(A[i]=read());
for(int i=0;i<n;++i)sb+=(B[i]=read());
if(sa<sb)A[0]+=sb-sa;
else B[0]+=sa-sb;
memcpy(oriA,A,sizeof(A));
memcpy(oriB,B,sizeof(B));
int cnt=0,tag=0;
for(int i=0,j=n-1;i<n;++i){
while(A[i]>0&&j>=0&&i+j>=n-1){
if(!B[j])--j;
else{
int delta=min(A[i],B[j]);
A[i]-=delta;
B[j]-=delta;
cnt+=delta;
if(i+j>=n)tag=1;
}
}
}
if(tag){
printf("%lld\n",cnt);
return;
}
int up=-inf,down=inf;
for(int i=0;i<n;++i){
if(A[i])chmax(up,i);
if(oriA[i]>A[i])chmin(down,i);
}
if(down<up)tag=1;
up=-inf,down=inf;
for(int i=0;i<n;++i){
if(B[i])chmax(up,i);
if(oriB[i]>B[i])chmin(down,i);
}
if(down<up)tag=1;
if(!tag)--cnt;
int FIND=-1;
for(int i=n-1;i>=0;--i)if(oriA[i]){
for(int j=n-1;j>=0;--j)if(oriB[j]){
FIND=i+j;
break;
}
if(FIND!=-1)break;
}
if(FIND<n){
puts("0");
return;
}
printf("%lld\n",cnt);
}
signed main(){
// freopen("test.out","w",stdout);
for(int cas=read();cas--;)solve();
}
详细
Test #1:
score: 100
Accepted
time: 5ms
memory: 15400kb
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
Time Limit Exceeded
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 1 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 3 3 2 2 1 0 1 2 2 2 0 3 0 2 1 1 0 1 0 4 0 0 2 2 0 3 2 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 2 1 1 0 0 0 1 0 3 2 1 0 2 1 1 0 1 0 0 1 3 1 3 3 2 2 1 0 2 0 0 2 3 1 3 1 0 1 1 3 0 1 2 0 1 1 ...