Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 5240 Accepted Submission(s): 3387
node
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.c++
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.this
Just output one line for one test case, as described in the Description.spa
Sempr上學的路上有一堆石子,每一個石子都有一個肯定的位置和能夠被扔出的距離,Sempr遇到第奇數個石子的時候會把它扔出去,第偶數個石子則不扔,若是在同一個位置有多個石子,扔出距離小的爲先遇到的,問被扔的最遠的石子在哪。code
這個題用優先隊列很好解決 維護一個Sempr要遇到石子的優先隊列,距離近的先遇到,距離相同的被扔出距離小的先遇到,第奇數個遇到的,將石子的位置加上扔出的距離,而後入隊,第偶數個遇到的就直接出隊了。orm
#include<bits/stdc++.h> using namespace std; typedef struct node { int add; int far; friend bool operator < (const node &a,const node b) { return a.add>b.add||(a.add==b.add&&a.far>b.far); } }stone; stone s; priority_queue<stone>q; int main() { int t,n,i,x,y; cin>>t; while(t--) { cin>>n; for(i=1;i<=n;i++) { cin>>x>>y; s.add=x; s.far=y; q.push(s); } int k=1,anss=0; while(!q.empty()) { if(k&1==1) { s=q.top(); s.add+=s.far; anss=max(s.add,anss); q.pop(); q.push(s); } else { s=q.top(); anss=max(s.add,anss); q.pop(); } k++; } cout<<anss<<endl; } }