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 }
时间: 2024-10-27 06:53:37

HDU 1896 Stones (优先队列)的相关文章

hdu 1896.Stones 解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目意思:给出 n 块石头的初始位置和能到达的距离.对于第奇数次遇到的石头才抛掷,偶数次的就忽略.问最多能扔到多远.如果有多颗石头在一个位置,距离小的那个标记为先遇到的. 所以先解说一下第二个测试案例: 2 1  5 6  6 在6这个位置的时候,由于5比6小,所以规定1(5)这个点是先遇上的,是偶数次遇到,所以忽略. 用优先队列做,位置近的优先级越高,如果位置相同,距离短的优先级越高. 1

hdu 1509 &amp; hdu 1873 &amp; hdu 1896 (基础优先队列)

http://acm.hdu.edu.cn/showproblem.php?pid=1509 裸的优先队列的应用,输入PUT的时候输入名字,值和优先值进队列,输入GRT的时候输出优先值小的名字和对应的值 注意的是优先级一样的时候输出顺序在前的 1 #include<cstdio> 2 #include<cstring> 3 #include<queue> 4 using namespace std; 5 struct point { 6 int val,odr,num;

HDU 1896 Stones

还是优先队列 #include<iostream> #include<cstdio> #include<cstring> #include<queue> using namespace std; #define maxn 100010 struct Node { int x,y,id; friend bool operator < (Node a,Node b) { if(a.x != b.x) return a.x > b.x; else if

HDU 1242 Rescue(优先队列+bfs)

题目地址:HDU 1242 这个题相比于普通的bfs有个特殊的地方,经过士兵时会额外消耗时间,也就是说此时最先搜到的时候不一定是用时最短的了.需要全部搜一遍才可以.这时候优先队列的好处就显现出来了.利用优先队列,可以让队列中的元素按时间排序,让先出来的总是时间短的,这样的话,最先搜到的一定是时间短的,就不用全部搜一遍了.PS:我是为了学优先队列做的这题..不是为了这题而现学的优先队列.. 代码如下: #include <iostream> #include <stdio.h> #i

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

hdoj 1896 Stones【优先队列】

Stones Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1363    Accepted Submission(s): 850 Problem Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every

Stones 优先队列

Stones 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 mee

2014年北京网络赛 Instrusive HDU 5040 题解 优先队列

网赛的时候看了这道题,发现就是平常的那种基础搜索题. 由于加了一个特殊条件:可以一次消耗3秒或原地停留1秒. 那就不能使用简单的队列了,需要使用优先队列才行. 题意 告诉一副地图:一个起点,一个终点,若干墙,若干监视器,剩下的是空地. 起点,终点,监视器都算空地. 监视器初始值会指定一个方向,共有四个方向. 监视器每秒顺时针转动到下个方向. 监视器视野距离为2. 在监视器的位置或在监视器面向的格子是监视区域. 普通的移动一格需要消耗1秒时间. 在监视器下移动一格需要消耗3秒时间. 如果呆在原地不

hdoj 1896 Stones

Stones Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1474    Accepted Submission(s): 921 Problem Description Because of the wrong status of the bicycle, Sempr begin to walk east to west every