QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#363996#8057. Best Carry Player 4ucup-team2219#RE 0ms3680kbC++141.7kb2024-03-24 09:22:482024-03-24 09:22:48

Judging History

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

  • [2024-03-24 09:22:48]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3680kb
  • [2024-03-24 09:22:48]
  • 提交

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) {
				assert(min_b == max_b);
				ans = 0;
			} else {
				assert(min_a < max_b && min_b < max_b);
				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: 3680kb

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:


result: