QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#625471 | #8058. Binary vs Ternary | tjf4# | RE | 0ms | 0kb | C++20 | 1.5kb | 2024-10-09 19:27:53 | 2024-10-09 19:27:54 |
answer
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
using namespace std;
typedef double db;
typedef long long ll;
typedef pair<ll,ll> pii;
typedef vector<pii> vii;
typedef vector<ll> vi;
typedef vector<string> vs;
typedef vector<char> vc;
const ll inf=1e18;
const int N=5e5+10;
ll m,a[N],b[N],c[N],as[N],bs[N];
int main() {
IOS
int T;
cin>>T;
while(T--) {
cin>>m; c[m]=0;
ll s1=0,s2=0;
for(int i=0;i<=m+1;i++) a[i]=b[i]=c[i]=as[i]=bs[i]=0;
for(int i=0;i<m;i++) cin>>a[i],s1+=a[i],as[i]=a[i];
for(int i=0;i<m;i++) cin>>b[i],s2+=b[i],bs[i]=b[i];
if(s1>s2) b[0]+=abs(s1-s2),bs[0]=b[0];
else a[0]+=abs(s1-s2),as[0]=a[0];
ll rf=m-1,sum=0,pd=0,fg=0;
for(int i=0;i<m;i++) {
if(rf<0) break;
if(a[i]>0&&b[rf]>0&&i+rf>=m) pd=1;
if(a[i]>0&&b[i]>0) fg++;
if(a[i]==b[rf]) {
sum+=a[i];
a[i]=b[rf]=0;
rf--;
}
else if(a[i]>b[rf]) {
sum+=b[rf];
a[i]-=b[rf];
b[rf]=0;
rf--;
if(i+rf<m-1);
else i--;
}
else if(a[i]<b[rf]) {
sum+=a[i];
b[rf]-=a[i];
}
}
for(int i=0;i<m;i++) a[i]=as[i];
for(int i=0;i<m;i++) b[i]=bs[i];
if(pd) cout<<sum<<'\n';
else if(fg>1) cout<<sum-1<<'\n';
else {
ll mx1=-1,mx2=-1;
for(int i=m-1;i>=0;i--) {
if(a[i]>0&&mx1<0) mx1=i;
if(b[i]>0&&mx2<0) mx2=i;
}
if(mx1>=0&&mx2>=0&&mx1+mx2>=m) pd=1;
if(pd==1) cout<<sum-1<<'\n';
else cout<<0<<'\n';
}
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Runtime Error
input:
3 1 111 110110 1101010 1111 111111