UVA 10954 【贪心+优先队列】

贪心思想是:数据排序的贪心方式
进行n-1次甲方 每次最小的俩个和进入队列,然后将每次和相加

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

struct node
{
    int x;
    bool operator<(const node &a)const
    {
        return x > a.x;//优先队列中和结构体排序不一样
    }
} num;

int main()
{
    int n;
    while(~scanf("%d",&n)&&n)
    {
        priority_queue<node>que;
        for(int i=0; i<n; i++)
        {
            scanf("%d",&num.x);
            que.push(num);
        }
        int sum=0;
        for(int i=0; i<n-1; i++)
        {
            node n1,n2;
            n1=que.top();
            que.pop();
            n2=que.top();
            que.pop();
            int t=n1.x+n2.x;
            sum+=t;
            num.x=t;
            que.push(num);
        }
        cout<<sum<<endl;
    }
}





猜你喜欢

转载自blog.csdn.net/Li_Hongcheng/article/details/86635534
今日推荐