QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#682784 | #8057. Best Carry Player 4 | ShiinaMahiru | WA | 59ms | 9796kb | C++17 | 2.4kb | 2024-10-27 17:21:33 | 2024-10-27 17:21:34 |
Judging History
answer
// #pragma GCC optimize(2)
#include<bits/stdc++.h>
#define endl '\n'
#define lowbits(x) ((x)&((-x)))
#define INF 0x3f3f3f3f
#define uu __int128
#define PI acos(-1)
#define ls k<<1
#define rs k<<1|1
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<int, pair<int, int> > PII;
constexpr int N=5e5+10, M=2*N;
constexpr int mod=998244353;
constexpr double eps=1e-9;
int nx[] = {0, 0, 1, -1}, ny[] = {1, -1, 0, 0};
int n;
int a[N], b[N];
int c[N], d[N];
void solve(){
cin >> n;
ll sz = 0, sz2 = 0;
for(int i=0; i<n; ++i)cin >> a[i], c[i] = a[i], sz += a[i];
for(int i=0; i<n; ++i)cin >> b[i], d[i] = b[i], sz2 += b[i];
if(sz > sz2){
b[0] += sz - sz2;
}
else a[0] += sz2 - sz;
c[0] = a[0];d[0] = b[0];
int mx = 0, mx2 = 0;
for(int i=0; i<n; ++i){
if(a[i])mx = i;
if(b[i])mx2 = i;
}
if(mx + mx2 < n){
cout << 0 << endl;return;
}
ll res = 0;
bool f = false;
for(int i=n-1, j=0; i>=0; --i){
if(!a[i])continue;
while(j < n && (!b[j] || i+j<n-1))++j;
if(j==n)break;
if(i+j >= n)f = true;
int d = min(a[i], b[j]);
a[i] -= d;
b[j] -= d;
res += d;
if(a[i])++i;
}
if(a[n-1])res += a[n-1];
if(b[n-1])res += b[n-1];
// for(int i=0; i<n; ++i)cout << a[i] << " \n"[i == n-1];
// for(int i=0; i<n; ++i)cout << b[i] << " \n"[i == n-1];
// cout << res << ' ' << f << endl;
if(f){
cout << res << endl;return;
}
ll ans = 0;
for(int i=0; i<n; ++i)a[i] = c[i], b[i] = d[i];
int t = 0;
for(int i=n-1, j=0; i>=0; --i){
if(!a[i])continue;
while(j < n && (!b[j] || i+j+t<n))++j;
if(j==n)break;
if(t == 0){
t = 1;
a[i]--;
b[j]--;
j = 0, i = n;
++ans;
continue;
}
int d = min(a[i], b[j]);
a[i] -= d;
b[j] -= d;
ans += d;
if(a[i])++i;
}
if(a[n-1] + t >= n)ans += a[n-1];
if(b[n-1] + t >= n)ans += b[n-1];
cout << ans << endl;
}
signed main()
{
ios::sync_with_stdio(0);cin.tie(0),cout.tie(0);
int t = 1;
// cout << fixed << setprecision(9)
cin >> t;
while(t--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 9724kb
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: 0
Accepted
time: 46ms
memory: 9796kb
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 ...
result:
ok 100000 numbers
Test #3:
score: 0
Accepted
time: 28ms
memory: 9792kb
input:
1000 500 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
0 1 1 2 2 2 0 0 0 3 1 1 2 0 1 0 1 1 3 0 0 4 3 1 4 4 0 0 2 1 3 3 0 0 1 0 1 1 0 0 2 1 2 0 3 1 3 1 3 4 2 1 1 2 3 0 0 1 2 0 0 1 1 1 1 2 1 2 2 0 2 2 1 1 3 0 0 1 2 0 1 3 0 0 1 1 0 0 1 0 3 1 2 0 0 5 1 1 1 3 1 4 1 0 0 0 0 0 1 3 1 2 1 0 0 0 0 0 1 1 0 4 0 1 1 3 0 1 0 0 2 3 0 1 2 1 1 1 0 3 0 0 3 2 1 1 0 1 0 1 ...
result:
ok 1000 numbers
Test #4:
score: -100
Wrong Answer
time: 59ms
memory: 9796kb
input:
100000 5 119777838 337555280 806504015 458289944 321614229 979242871 431783189 423329635 193632121 7339369 5 189264782 667669243 753322761 861814678 224007583 977519325 104432095 940220826 712094722 903574615 5 977791540 278658984 601762324 633391706 36807634 689562032 997406456 118939957 891425821 ...
output:
1377698543 2884329841 1699584169 2132012702 2531472710 2754014935 2585135038 2577893498 2034726862 2303377730 1476887044 2618960687 1626312268 1068946376 600795101 927483202 2151016849 2256729729 2291627593 1751199962 2412405837 1884010446 2553701265 2014856631 848686688 1069641213 2412585811 167144...
result:
wrong answer 39th numbers differ - expected: '1572566306', found: '-2722400990'