hdu1896Stones

来源:互联网 发布:被优化掉是什么 编辑:程序博客网 时间:2024/06/07 09:11

Stones

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


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

 

题目类型:优先队列

题意理解:小明上学跟放学回家,(假定如此,题目只说由东向西)的路上很无聊很累所以他就没事扔石子,遇到序号为奇数时小明就会仍石头,遇到序号为偶数时小明不管他,石子有序号例如测试数据1  5代表序号1,2  4代表序号2……1代表石子序号1所在的位置,2代表石子序号2所在的位置,但序号跟位置是没有关系的(等下解释第二组数据会讲到),5和4代表他能将该对应石子扔的最远距离,当位置相同时,小明就会捡最大的扔,也就是仍距离最小的,(有力气就是这么任性)比如第二组数据,1 5是序号1,扔出去就变成6  5,但是现在有两组数据6 5(序号3)跟6 6(序号2),由于位置都在6,所以小明要扔大的扔6 6,此时你也就明白为什么序号跟那个位置没关系了,变成12 6,次数序号为4,因此不做处理。

做题:定义结构体优先队列,定义优先级,在位置相同的情况下重量大的优先级大,否则位置小的优先级大;由于他是序列为奇数时扔石子,偶数时不仍石子,因此我把第一个仍了以后存入优先队列,pop两次(一次是你扔过的石子要把它弹出去),下一次是序号为偶数的石子不做处理弹出去,,一直处理到最后一个,输出他的位置即可;

 

<pre class="cpp" name="code">#include<iostream>#include<queue>#include<cstdio>#include<cstring>using namespace std;struct node{int P;//位置int D;//重量bool operator < (const node a) const{if(P==a.P)//在位置相同的情况下重量大的优先级大return a.D<D;return P>a.P;//位置小的优先级大;}}temp,t;priority_queue <node> q;int main(){int T,n,i,j,k;scanf("%d",&T);while(T--)//控制次数{while(!q.empty())//清空q.pop();scanf("%d",&n);for(i=1;i<=n;i++)//存入队列{scanf("%d%d",&temp.P,&temp.D);q.push(temp);}while(!q.empty()){t.D=q.top().D;t.P=q.top().P+q.top().D;q.push(t);q.pop();//弹出去两次q.pop();}cout<<q.top().P<<endl;}return 0;}/*541 21 32 52 4221 56 6  */


 

 

 

 

0 0