贪心问题

版权声明:版权所有,转载请标明 https://blog.csdn.net/zxwsbg/article/details/81735367

切金条

题目链接

          https://www.luogu.org/problemnew/show/P1090

题意

         合并果子啊。。

题解

          采用小根堆的思想,每次取出数组里的最小的两个合并,再加进数组。。依次类推。

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

#define INIT(x) memset(x,0,sizeof(x))
#define eps 1e-8

typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 200005;


priority_queue<int,vector<int>,greater<int> >q;
int n;

int main()
{
	while(cin>>n) {
		int ans = 0;
		for(int i=0;i<n;i++) {
			int x;
			cin>>x;
			q.push(x);
		}
		while(q.size()>1) {
			int x = q.top();
			q.pop();
			int y = q.top();
			q.pop();
			ans += x+y;
			q.push(x+y);
		} 
		cout<<ans<<endl;
		q.pop();
	}
	return 0;
}



猜你喜欢

转载自blog.csdn.net/zxwsbg/article/details/81735367