zoukankan      html  css  js  c++  java
  • HDU 1896 Stones (优先队列)

    Problem Description

    Because of the wrong status of the bicycle, Sempr begin to walk east to west every morning and walk back every evening. Walking may cause a little tired, so Sempr always play some games this time. 
    There are many stones on the road, when he meet a stone, he will throw it ahead as far as possible if it is the odd stone he meet, or leave it where it was if it is the even stone. Now give you some informations about the stones on the road, you are to tell me the distance from the start point to the farthest stone after Sempr walk by. Please pay attention that if two or more stones stay at the same position, you will meet the larger one(the one with the smallest Di, as described in the Input) first. 

    Input

    In the first line, there is an Integer T(1<=T<=10), which means the test cases in the input file. Then followed by T test cases. 
    For each test case, I will give you an Integer N(0<N<=100,000) in the first line, which means the number of stones on the road. Then followed by N lines and there are two integers Pi(0<=Pi<=100,000) and Di(0<=Di<=1,000) in the line, which means the position of the i-th stone and how far Sempr can throw it.

    Output

    Just output one line for one test case, as described in the Description.

    Sample Input

    2
    2
    1 5
    2 4
    2
    1 5
    6 6
    

    Sample Output

    11
    12
    

    Author

    Sempr|CrazyBird|hust07p43

    Source

    HDU 2008-4 Programming Contest
     1 #include<iostream>
     2 #include<queue>
     3 using namespace std;
     4 struct node//石头
     5 {
     6     int pos,dis;//pos表示位置,dis表示距离
     7 };
     8 struct cmp//queue的比较函数
     9 {
    10     bool operator()(node a,node b)
    11     {
    12         if(a.pos==b.pos)return a.dis>b.dis;
    13         return a.pos>b.pos;//top是最小
    14     }
    15 };
    16 int n,num;//n表示石头的个数,num表示遇到的石头序号
    17 int main()
    18 {
    19     int T;
    20     cin>>T;
    21     while(T--)
    22     {
    23         cin>>n;
    24         priority_queue<node,vector<node>,cmp>Q;
    25         node stone;
    26         for(int i=0;i<n;i++)
    27         {
    28             cin>>stone.pos>>stone.dis;
    29             Q.push(stone);
    30         }
    31         num=0;
    32         while(!Q.empty())//没有可投掷的石头时,一直pop top
    33         {
    34             stone=Q.top();
    35             Q.pop();
    36             num++;
    37             if(num%2==1)
    38             {
    39                 stone.pos+=stone.dis;
    40                 Q.push(stone);
    41             }
    42         }
    43         cout<<stone.pos<<endl;
    44     }
    45     return 0;
    46 }
  • 相关阅读:
    java工厂方法模式
    java简单工厂设计模式
    Springboot接口简单实现生成MySQL插入语句
    JMeter 源码二次开发函数示例
    AssertJ断言系列-----------<数据库断言三>
    钉钉机器人集成Jenkins推送消息模板自定义发送报告
    删除ORECLE表主键ID的索引
    日志——log4j.properties配置文件说明
    java基础——反射机制(reflect)的使用
    spring batch (四) Job的配置及配置文件说明介绍
  • 原文地址:https://www.cnblogs.com/Annetree/p/5662515.html
Copyright © 2011-2022 走看看