其他分享
首页 > 其他分享> > poj 3253 Fence Repair

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;
}

标签:Repair,int,ll,3253,pop,poj,ans,include,define
来源: https://www.cnblogs.com/wzxbeliever/p/16301678.html