hdu1896 Stones 优先队列使用

来源:互联网 发布:澳门酒店攻略知乎 编辑:程序博客网 时间:2024/06/06 01:37

Stones

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 2672    Accepted Submission(s): 1710


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
221 52 421 56 6
 

Sample Output
1112


题意比较简单,走在路上,他会遇到一些石头,给出石头的个数,输入石头的出现位置,和可以被扔出去的距离

求遇到最后一个石头,该石头距离初始位置的距离。

you will meet the larger one(the one with the smallest Di, as described in the Input)

这句话,遇到同一位置多个石头,取扔的近的扔。

利用优先队列模拟过程,将遇到的时间与石头可被扔的距离作为优先级、

代码如下

#include<iostream>#include<queue>using namespace std;struct Node{int p;int d;};struct cmp{bool operator ()(const Node &a,const Node &b){//最小值优先 if(a.p!=b.p){return a.p>b.p;}else{ return a.d>b.d;}}};int  main(){int t,n;cin>>t;while(t--){priority_queue<Node,vector<Node>,cmp> stone;cin>>n;int p,d;Node node;for(int i=0;i<n;i++){cin>>p>>d;node.p = p;node.d = d;stone.push(node);}//遇到的第一个石头为奇数。 int start = 1;Node get;while(!stone.empty()){get = stone.top();stone.pop();if(start){get.p+=get.d;stone.push(get);}//实现奇偶交替 start = !start;}cout<< get.p<<endl;}return 0;}



0 0
原创粉丝点击