QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#494118#9142. Uniting Amoebasucup-team3661#WA 116ms3844kbC++201.1kb2024-07-27 14:14:452024-07-27 14:14:45

Judging History

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

  • [2024-07-27 14:14:45]
  • 评测
  • 测评结果:WA
  • 用时:116ms
  • 内存:3844kb
  • [2024-07-27 14:14:45]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
	int T;cin>>T;

	while(T--){
		int N;cin>>N;
		vector<ll>A(N);
		for(int i=0;i<N;i++){cin>>A[i];}

		multiset<tuple<ll,int,int,int>>pq;
		vector<int>L(N),R(N);
		for(int i=0;i<N;i++){
			pq.insert({A[i],i,(i-1+N)%N,(i+1)%N});
			L[i]=(i-1+N)%N;
			R[i]=(i+1)%N;
		}

		ll ans=0;

		auto solve=[&](int l,int r){
			if(pq.size()==2){
				ans+=min(A[l],A[r]);
				pq.erase(pq.begin());
				return;
			}

			assert(L[r]==l&&R[l]==r);
			int rr=R[r];

			pq.erase(pq.find({A[l],l,L[l],R[l]}));
			pq.erase(pq.find({A[r],r,L[r],R[r]}));
			pq.erase(pq.find({A[rr],rr,L[rr],R[rr]}));

			ans+=min(A[l],A[r]);
			A[l]+=A[r];

			R[l]=rr;
			L[rr]=l;
			pq.insert({A[l],l,L[l],R[l]});
			pq.insert({A[rr],rr,L[rr],R[rr]});
		};

		while(pq.size()>1){
			auto tup=*pq.begin();
			auto[ai,i,l,r]=tup;
			//cerr<<pq.size()<<'\n';
			//cerr<<ai<<' '<<i<<' '<<l<<' '<<r<<"\n\n";

			if(A[l]<A[r]){
				solve(l,i);
			}

			else{
				solve(i,r);
			}
		}

		cout<<ans<<'\n';
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
3
1 1 1
4
0 1 0 2
2
100 42

output:

2
1
42

result:

ok 3 number(s): "2 1 42"

Test #2:

score: 0
Accepted
time: 93ms
memory: 3512kb

input:

100000
2
653035426 456936267
2
409660907 250295986
2
347384960 200356123
2
283557883 840123319
2
17610671 987402881
2
136895857 909667348
2
248911778 780375903
2
275329617 285342631
2
561813712 698064200
2
400903421 742043963
2
345893112 519641162
2
14593307 406479738
2
616391850 347632216
2
1973087...

output:

456936267
250295986
200356123
283557883
17610671
136895857
248911778
275329617
561813712
400903421
345893112
14593307
347632216
19730879
421626272
56181583
370600905
308183211
258362299
586922640
40159075
198139063
142754581
51756812
119730671
81065481
143568225
273158312
49357396
14434997
396080587...

result:

ok 100000 numbers

Test #3:

score: -100
Wrong Answer
time: 116ms
memory: 3844kb

input:

10000
17
682602980 427465994 308629394 109001734 104676154 428796022 186854021 449599165 926341494 518520271 848737248 161898549 472666174 717817465 55045664 497142577 155911030
17
175921543 574867131 81827354 668974927 924162354 915237015 544633396 979579177 69438027 7405300 106382140 464417239 180...

output:

11530543806
13350411326
4922176567
455185902
50552732518
3781245449
10842444270
92913551068
36575602322
6158957338
4035289647
6326418001
21135916989
12292585520
7820337664
33316192662
3946551868
7013223204
14047204357
3685850783
33140035344
11319298052
20395006172
1572801098
1238621062
4942166103
11...

result:

wrong answer 1st numbers differ - expected: '6125364442', found: '11530543806'