QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#364003#8057. Best Carry Player 4ucup-team2219#WA 72ms3752kbC++142.0kb2024-03-24 09:34:042024-03-24 09:34:04

Judging History

你现在查看的是最新测评结果

  • [2024-03-24 09:34:04]
  • 评测
  • 测评结果:WA
  • 用时:72ms
  • 内存:3752kb
  • [2024-03-24 09:34:04]
  • 提交

answer

#include <bits/stdc++.h>
#define rep(i,a,b) for(ll i = (a); i < (b); i++)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

const ll INF = 0x3f3f3f3f3f3f3f3f;
void solve() {
	ll m;
	scanf("%lld", &m);
	vector<ll> a(m), b(m);
	ll sum_a = 0, sum_b = 0;
	for(auto &g: a) scanf("%lld", &g), sum_a += g;
	for(auto &g: b) scanf("%lld", &g), sum_b += g;
	a[0] += max(sum_a, sum_b) - sum_a;
	b[0] += max(sum_a, sum_b) - sum_b;
	ll ans = 0, has_carry = false;
	stack<pll> stk;
	ll min_a = INF, min_b = INF, max_a = -1, max_b = -1;
	rep(i,0,m) {
		stk.emplace(m-1-i, b[m-1-i]);
		// cout<<"#"<<i<<endl;
		while(!stk.empty() && a[i] > 0) {
			auto [pos, amount] = stk.top();
			ll take = min(amount, a[i]);
			// cout<<pos<<" "<<amount<<"  "<<take<<endl;
			ans += take;
			a[i] -= take;
			b[pos] -= take;
			stk.pop();
			if(amount > take) {
				// cout<<"#F"<<pos<<"  "<<amount-take<<endl;
				stk.emplace(pos, amount-take);
			}
			min_a = min(min_a, i);
			min_b = min(min_b, pos);
			max_a = max(max_a, i);
			max_b = max(max_b, pos);
			if(i + pos >= m) {
				has_carry = true;
			}
		}
	}
	// cout<<"C"<<has_carry<<endl;
	if(!has_carry) {
		bool good = false;
		rep(i,0,m) {
			if(a[i] > 0 && i > min_a) good = true;
			if(b[i] > 0 && i > min_b) good = true;
		}
		if(!good) {
			if(min_a == INF || min_b == INF || max_a == -1 || max_b == -1) {
				assert(min_a == INF && min_b == INF && max_a == -1 && max_b == -1);
				assert(ans == 0);
			} else if(min_a == max_a && min_b == max_b) {
				bool ok = false;
				rep(i,min_a+1,m) if(a[i] > 0) {
					ok = true;
				}
				rep(i,min_b+1,m) if(b[i]>0) ok=true;
				if(!ok)	ans = 0;
			} else {
				// assert(min_a < max_b && min_b < max_b);
				bool ok = false;
				rep(i,min_a+1,m) if(a[i] > 0) ok = true;
				rep(i,min_b+1,m) if(b[i] > 0) ok = true;
				if(!ok) ans--;
			}
		}
	}
	cout<<ans<<'\n';
}
int main() {
	int T;
	scanf("%d", &T);
	while(T--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3752kb

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
Wrong Answer
time: 72ms
memory: 3704kb

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
1
1
0
0
0
0
0
4
1
4
1
0
2
3
3
1
5
0
0
2
0
1
1
1
0
1
3
5
3
2
2
2
0
1
2
2
2
0
3
1
2
1
1
0
1
0
4
2
2
2
2
0
3
3
0
2
0
1
0
1
2
1
2
0
3
4
0
2
5
0
2
1
1
1
0
3
2
3
0
2
0
4
3
3
0
2
2
0
1
3
1
1
0
1
0
1
0
3
2
2
0
2
1
1
1
2
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:

wrong answer 16th numbers differ - expected: '0', found: '1'