QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#600129#9142. Uniting AmoebasANIG#RE 0ms0kbC++141.2kb2024-09-29 14:54:262024-09-29 14:54:26

Judging History

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

  • [2024-09-29 14:54:26]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:0kb
  • [2024-09-29 14:54:26]
  • 提交

answer

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

void solve(){
	int n;
	cin>>n;
	priority_queue<pair<E,int>,vector<pair<E,int>>,greater<>> pq;
	multiset<pair<int,E>> st;
	for(int i=0; i<n; i++){
		int x;
		cin>>x;
		st.insert(make_pair(i,x));
		pq.push(make_pair(x,i));
	}
	
	E ans=0;
	vector<int> stt(n+1);
	while(pq.size()){
		int u=pq.top().second; pq.pop();
		if(stt[u]) continue;
		auto pnt=st.lower_bound(make_pair(u,0));
		auto pnt1=(pnt==st.begin()?prev(st.end()):prev(pnt));
		auto pnt2=(next(pnt)==st.end()?st.begin():next(pnt));
		ans+=pnt->second;
		if(pnt1->second>pnt2->second){
			st.insert(make_pair(pnt1->first,pnt1->second+pnt->second));
			pq.push(make_pair(pnt1->second+pnt->second,pnt1->first));
			stt[pnt->first]=1;
			st.erase(pnt1);
			st.erase(pnt);
		}
		else{
			st.insert(make_pair(pnt2->first,pnt2->second+pnt->second));
			pq.push(make_pair(pnt2->second+pnt->second,pnt2->first));
			stt[pnt->first]=1;
			st.erase(pnt2);
			st.erase(pnt);
		}
	}
	
	cout<<ans<<'\n';
}

signed main(){
	
	cin.tie(0),cout.tie(0)->sync_with_stdio(false);
	
	int T;
	cin>>T;
	while(T--) solve();
	
	return 0;
}

详细

Test #1:

score: 0
Runtime Error

input:

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

output:


result: