您的位置:首页 > 其它

HDU 1896 Stones ——STL 优先队列

2014-04-12 15:39 435 查看
题目链接点击打开链接

这道题,就是用优先队列把按照题目要求把整个过程模拟处理就好了 需要注意的一点就是优先队列默认从大到小排列 要自定义排列顺序 用到运算符的重载了

#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iomanip>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
using namespace std;
struct node
{
    friend bool operator< (node n1, node n2)//自定义排序
    {
        if(n1.p==n2.p)
            return n1.d>n2.d;
        else
            return n1.p>n2.p;
    }
    int p;
    int d;
};
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        priority_queue<node> q;
        int n,i;
        cin>>n;
        for(i=0; i<n; i++)
        {
            node tn;
            cin>>tn.p>>tn.d;
            q.push(tn);
        }
        int cnt=1,distence=0;
        while(!q.empty())
        {
            distence=q.top().p;
            if(cnt%2==0)
            {
                q.pop();
            }
            else
            {
                node temp=q.top();
                temp.p+=temp.d;
                q.pop();
                q.push(temp);
            }
            cnt++;
            distence=q.top().p;
        }
        cout<<distence<<endl;
    }

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