hdu4302 Holedox Eating(multiset)

Holedox Eating

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 983    Accepted Submission(s): 335


Problem Description
Holedox is a small animal which can be 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 in the pipe, from time to time. When Holedox wants to eat cakes, it always goes to the nearest one and eats it. If there are many pieces of cake in different directions Holedox can choose, Holedox will choose one in the direction which is the direction of its last movement. If there are 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 <= 10), the number of test cases, followed by the input data for each test case.The first line of each case contains two integers L,n(1<=L,n<=100000), representing the length of the pipe, and the number of events.
The next n lines, each line 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.
In each case, Holedox always starts off at the position 0.
 

 

Output
Output the total distance Holedox will 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
Source
 
题意:在坐标[0,n]上会随机出现一些cake,输入 0 x  表示在坐标 x 处有一个cake,1 表示吃掉离当前最近的一个cake,如果左右相等则和上次移动方向一致, 初始位置在0处。
 
算法分析:STL中的multiset
 
View Code
 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #include<set>
 5 #define MIN -20000000
 6 #define MAX 20000000
 7 
 8 using namespace std;
 9 
10 int main()
11 {
12     int test,n,m,i,k=1,key,t,p,ans,now;
13     scanf("%d",&test);
14     while(test--)
15     {
16         multiset<int>ms; ms.clear();
17         multiset<int>::iterator it,ita,itb;
18         scanf("%d %d",&n,&m);
19         ms.insert(MIN);
20         ms.insert(MAX);
21         ans=0;p=1;now=0;
22         for(i=0;i<m;i++)
23         {
24             scanf("%d",&key);
25             if(key==0)
26             {
27                 scanf("%d",&t);
28                 ms.insert(t);
29             }
30             else
31             {
32                 ita=itb=ms.lower_bound(now);//大于等于now的最小值
33                 ita--;
34                 if(*itb==now)
35                 {
36                     ms.erase(itb);continue;
37                 }
38                 if(*ita==MIN&&*itb==MAX) continue;
39                 if(now-*ita<*itb-now)
40                 {
41                     ans+=(now-*ita);p=0;now=*ita;
42                     ms.erase(ita);
43                 }
44                 else if(now-*ita>*itb-now)
45                 {
46                     ans+=(*itb-now);p=1;now=*itb;
47                     ms.erase(itb);
48                 }
49                 else
50                 {
51                     ans+=(now-*ita);
52                     if(p==0)
53                     {
54                         now=*ita;ms.erase(ita);
55                     }
56                     else
57                     {
58                         now=*itb;ms.erase(itb);
59                     }
60                 }
61             }
62         }
63         printf("Case %d: %d\n",k++,ans);
64     }
65     return 0;
66 }

 

posted @ 2012-07-21 16:26  mtry  阅读(382)  评论(0)    收藏  举报