Stones (priority queue)

Source: Internet
Author: User

Stones

Time limit:5000/3000 MS (java/others) Memory limit:65535/32768 K (java/others)
Total submission (s): 1373 Accepted Submission (s): 858


Problem Descriptionbecause of the wrong status of the bicycle, SEMPR begin to walk east to West every morning and walk BAC K every evening. Walking may cause a little tired, so sempr always play some games this time.
There was many stones on the road, when he meet a stone, he would throw it ahead as far as possible if it was the odd stone He meet, or leave it where it was if it's the the even stone. Now give your some informations about the stones on the road and you is to tell me the distance from the start point to the F Arthest Stone after Sempr walk by. Please pay attention this if or more stones stay at the same position, you'll meet the larger one (the one with the S Mallest Di, as described in the Input) first.

Inputin the first line, there is a 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'll give you a Integer N (0<n<=100,000) in the first line, which means the number of stones O n the road. Then followed by N lines and there is the 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.

Outputjust output one line to one test case, as described in the Description.

Sample Input2 2 1 5 2 4 2 1 5 6 6

Sample Output11 12 Test instructions: According to the first few stones, the odd number of stones to throw, the same location of the stones to be thrown from the distance from small to large sort; Use the priority queue; Code:
1#include <stdio.h>2#include <queue>3 using namespacestd;4 structnode{5     intPosition,dis;6FriendBOOL operator<(Node A,node b) {7         if(a.position!=b.position)returnA.position>b.position;8         Else returnA.dis>B.dis;9     }Ten }; One intMain () { A     intt,n,k; - Node m; -scanf"%d",&T); the      while(t--) {k=0;p riority_queue<node>Stone; -scanf"%d",&N); -          while(n--) scanf ("%d%d",&m.position,&M.dis), Stone.push (m); -          while(!stone.empty ()) {k++; +             if(k&1) M=stone.top (), m.position+=M.dis,stone.push (m); - Stone.pop (); +             if(Stone.empty ()) printf ("%d\n", Stone.top (). position); A         } at     } -     return 0; -}
View Code

Stones (priority queue)

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.