Hdoj 4302 holedox Eating "Multiset"

Source: Internet
Author: User

Holedox Eating
Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 3430 Accepted Submission (s): 1178

Problem Description

Holedox is a small animal which can being considered as one point. It lives in a straight pipe whose length is L. Holedox can only move along the pipe. Cakes may appear anywhere on the pipe, from time to time. When Holedox wants to eat cakes, it always goes to the nearest one and eats it. If There is many pieces of cake in different directions holedox can choose, Holedox'll choose one in the direction whic H is the direction of it last movement. If There is no cakes present, holedox just stays where it is.

Input

The input consists of several test cases. The first line of the input contains a single integer t (1 <= t <=), the number of test cases, followed by the Put data for each test case. The first line of each case contains the integers l,n (1<=l,n<=100000), representing the length of the pipe, and the Number of events.
The next n lines, each of the describes an event. 0 x (0<=x<=l, x is a integer) represents a piece of cake appears in the X position; 1 represent Holedox wants to eat a cake.
Holedox always starts off at the position 0.

Output

Output the total distance holedox'll move. Holedox don ' t need to return to the position 0.

Sample Input

3
10 8
0 1
0 5
1
0 2
0 0
1
1
1

10 7
0 1
0 5
1
0 2
0 0
1
1

10 8
0 1
0 1
0 5
1
0 2
0 0
1
1

Sample Output

Case 1:9
Case 2:4
Case 3:2

Author

Bupt

Source

Multi-university Training Contest 1
Test instructions: In a straight line, starting from 0, each time is to eat the nearest point, each time can only eat one, in which place to eat, after eating in that position standing. No cake, standing in situ, if the nearest distance from the left and right, just follow the direction of the original walk.
Use a method Lower_bound () to return an iterator that is greater than or equal to the key value
Code:

#include <iostream>#include <cstdio>#include <set>using namespace STD;intMain () {intT, V =1;scanf("%d", &t); while(T-) {intL, N;scanf("%d%d", &l, &n); multiset<int >Aintc, B;intsum =0, cur =0, dir =0; for(inti =0; I < n; ++i) {scanf("%d", &c);if(c = =0){scanf("%d", &b);            A.insert (b); }Else{if(a.size () = =0)Continue; multiset<int >:: Iterator it = a.lower_bound (cur);if(It = = A.end ()) {--it;intWW = (*it); DIR =-1;                        Sum + = (cur-(*it));                        Cur = (*it);                A.erase (IT); }Else if((*it) = = cur) {intWW = (*it);//cur = (*it);A.erase (IT); }Else{if(It = = A.begin ()) {intWW = (*it); DIR =1;                        Sum + = (*it)-cur;                        Cur = (*it);                    A.erase (IT); }Else{intWW = (*it); multiset<int >:: iterator d = it; --d;intQQQQ = (*d);intTe = (cur-(*d));inttemp = ((*it)-cur);if(Te = = temp) {if(Dir >0) {sum + = temp;                                Cur = (*it);                            A.erase (IT); }Else{sum + = temp;                                Cur = (*d);                            A.erase (d); }                        }Else if(Te < temp) {dir =-1;                            sum + = Te;                            Cur = (*d);                        A.erase (d); }Else{dir =1;                            sum + = temp;                            Cur = (*it);                        A.erase (IT); }                    }                }            }        }printf("Case%d:%d\n", v++, sum); }return 0;}

Hdoj 4302 holedox Eating "Multiset"

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.