码迷,mamicode.com
首页 > 其他好文 > 详细

带结构体的优先队列

时间:2017-11-16 22:09:23      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:tmp   mes   思路   can   相同   empty   pop   algorithm   遇到   

hdu-1896

问题描述:路上有一些石头(位置,所抛出的距离),如果遇到的是奇数块石头,则向前抛,偶数块石头就让它放在原地不管。如果遇到位置相同的两块石头,就假设抛出距离近的石头先遇到。

思路:遇到奇数块石头,(位置=位置+距离,距离)入栈,偶数则不管,直至栈空为止


#include <cstdio>
#include <queue>
#include <vector>
using namespace std;

struct Node
{
    int p,d;
}node;

struct cmp
{
    bool operator() (Node &x,Node &y)
    {
        if( x.p== y.p) return x.d > y.d;
        return x.p > y.p;
    }
};
int main()
{
    int T,n;
    scanf("%d",&T);
    priority_queue<Node,vector<Node>,cmp> que;
    while(T--)
    {
        scanf("%d",&n);
        for(int i = 0; i< n;i++)
        {
            scanf("%d%d",&node.p,&node.d);
            que.push(node);
        }
        int cnt = 0;
        while(!que.empty())
        {
            cnt++;
            node = que.top();
            que.pop();
            if(cnt%2 == 1)
            {
                node.p+=node.d;
                que.push(node);
            }
        }
        printf("%d\n",node.p);
    }
    return 0;
}
/*
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
struct Stone{
    int x;    //石头的初始地
    int y;    //石头能扔的最远距离
};
bool operator<( Stone a, Stone b )
{
    if( a.x== b.x ) return a.y > b.y;
    return a.x > b.x;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n;
        int i ;
        priority_queue<Stone>que;
        scanf("%d",&n);
        Stone tmp;
        for(i =0;i< n ; i++ )
        {
            scanf("%d%d",&tmp.x,&tmp.y);
            que.push(tmp);
        }
        int sum =1;
        while(!que.empty())
        {
        tmp = que.top();
            que.pop();
            if(sum%2)
            {
                tmp . x+=tmp.y;
                que.push(tmp);
            }
            sum++;
        }
        printf("%d\n",tmp.x);
    }
    return 0;
}
*/

带结构体的优先队列

标签:tmp   mes   思路   can   相同   empty   pop   algorithm   遇到   

原文地址:http://www.cnblogs.com/unknownname/p/7846335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!