zoukankan      html  css  js  c++  java
  • 带结构体的优先队列

    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 ",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 ",tmp.x);
        }
        return 0;
    }
    */

  • 相关阅读:
    【HDU5015】233 Matrix
    【POJ3233】Matrix Power Series
    【POJ3070】Fibonacci
    【NOIP模拟】奇怪的字符串
    【NOIP模拟】超级跳棋
    【NOIP模拟】玛雅文字
    【NOIP模拟】距离
    【闲聊】关于本BLOG
    【NOIP模拟】树
    【NOIP模拟】机器人
  • 原文地址:https://www.cnblogs.com/unknownname/p/7846335.html
Copyright © 2011-2022 走看看