您的位置:首页 > 其它

BUN 1020--信息战(六)——军队体检 ——优先队列

2014-07-03 15:57 134 查看
题目链接点击打开链接

使用优先队列

复习优先队列自定义排序函数以及pop push top等操作。

AC代码

#include<iostream>
#include<algorithm>
#include<iomanip>
#include<queue>
using namespace std;

struct node

{
    friend bool operator <(node x,node y)
    {
        return x.value > y.value;
    }
    double value;
};
bool cmp(node a, node b)
{
    return a.value>b.value;
}
node a[1000005];

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m,p,i,j,sum=0;
        cin>>n>>m>>p;
        for(i=0; i<n; i++)
        {
            cin>>a[i].value;
        }
        sort(a,a+n,cmp);
        priority_queue<node> q;
        for(i = 0; i<m; i++)
        {
            q.push(a[i]);
        }

        for(i=m; i<n; i++)
        {
            node tmp=q.top();
            q.pop();
            tmp.value+=a[i].value;
            q.push(tmp);
        }
        node tmp;
        while(!q.empty())
        {
            tmp=q.top();
            q.pop();

        }
        cout<<fixed<<setprecision(3)<<tmp.value/p<<endl;

    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: