Hdu1896 priority queue 2

Source: Internet
Author: User
D-priority queue entry 2 Time limit:3000 Ms Memory limit:32768kb 64bit Io format:% I64d & % i64usubmit status practice HDU 1896

Description

Because of the wrong status of the bicycle, seflat begin to walk east to west every morning and walk back every evening. Walking may cause a little tired, so seflat always play some games this time.
There are running 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 sepr walk. 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 seflat 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 
 

There are a lot of stones on the road. When you encounter an odd sequence of stones, you will move him forward, and the even number will not move him. If the two stones are together, first, consider the relatively close stone that can still be a relatively large stone. This way, continue until all the stones in front can still be.

In fact, it is to coordinate this process, Pi is the coordinate, and Di is several units of length.

Here, the priority queue uses the custom struct method:

struct point{int dis,pos;friend bool operator <(point a,point b){if(a.pos==b.pos)return a.dis>b.dis;return a.pos>b.pos;}};
Similarly, the question "if two stones are together, first consider that the close stone is still a relatively large stone", so there should be if (. pos = B. pos) return. dis> B. DIS;

As for the '<' and '>' judgment in the return statement, here we can make a simple summary:

When the maximum priority queue is used, use the symbol <comparison:

When it is the smallest priority queue, use the symbol> for comparison.

In this question, the distance and position are both relatively small, so both return statements are used <;

The full code is as follows:

#include <stdio.h>#include <string.h>#include <queue>using namespace std;struct point{int dis,pos;friend bool operator <(point a,point b){if(a.pos==b.pos)return a.dis>b.dis;return a.pos>b.pos;}}t;int main(){int i,n,m,p,d;scanf("%d",&m);priority_queue<point>q;while(m--){scanf("%d",&n);while(!q.empty())q.pop();for(i=0;i<n;i++){scanf("%d%d",&t.pos,&t.dis);q.push(t);}int ans=0,count=1;//point p;while(!q.empty()){if(count&1){t=q.top();q.pop();t.pos+=t.dis;ans=t.pos;q.push(t);}elseq.pop();count++;}printf("%d\n",q.top().pos);}return 0;}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.