poj 3253 Fence Repair

分析:发现越到后面分出去 它对整个答案的贡献加的次数就越多 所以我们想尽可能最小的最后分出去

找到当前最小和次小的删去 两者合并 再加入原序列中 依次这样操作就可

#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
#define lowbit(x) x&(-x)
#define ll long long
#define int ll
int n,ans;
priority_queue<ll,vector<ll>,greater<ll> >Q;
signed main(){
	cin>>n;
	for(int i=1;i<=n;i++){
		int x;scanf("%lld",&x);
		Q.push(x);
	}
	while(Q.size()>1){
		int u=Q.top();Q.pop();
		u+=Q.top();Q.pop();
		ans+=u;
		Q.push(u);
	}
	cout<<ans<<endl;
     return 0;
}

posted @ 2022-05-23 16:22  wzx_believer  阅读(25)  评论(0)    收藏  举报