Stones
Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 1396 Accepted Submission(s): 873
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.
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.
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恩,题目大意就是说一个人走路太淘气,看到路上的石子,如果是第奇数个就把它捡起来尽力的把它扔远,如果是第偶数个就无视它。输入时N表示测试数据组数,然后n表示石子的个数,然后两个数分别表示该石子在路上的位置和能扔的距离。若在同一点处有不止一个石子就扔那个最大的即扔的最近的。问他路过的最后一个石子距离出发点多远。优先队列若位置不同按位置从小到大排,若相同按扔的距离从小到大排,然后第奇数个位置更被删除,添加其原来位置加上扔的距离,其他不变。偶数删除,直到队列只有一个元素,输出该元素的位置加上扔的距离即可。#include<cstdio> #include<cstring> #include<queue> #include<algorithm> using namespace std; struct node { int pi; int di; int num; friend bool operator < (node a,node b) { if(a.pi!=b.pi)return a.pi>b.pi; else return a.di>b.di; } }t; int main() { int m,n,i,cnt,sum; scanf("%d",&m); while(m--) { priority_queue<node>q; scanf("%d",&n); cnt=0; while(n--) { cnt++; scanf("%d%d",&t.pi,&t.di); t.num=cnt; q.push(t); } while(!q.empty()) { t=q.top();//从第一个开始一定是奇数 t.pi=t.pi+t.di; q.pop();//删除旧的 q.push(t);//添加新的 if(!q.empty())q.pop();//若此时队列不空,则删除第偶数个 if(q.size()==1)//判断若队列只剩一个元素,则跳出循环,输出结果 { t=q.top(); sum=t.pi+t.di; break; } } printf("%d\n",sum); } return 0; }