Holedox Eating HDU

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. 

InputThe 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. 
OutputOutput 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
两个优先队列 voer
  1 #include <iostream>
  2 
  3 using namespace std;
  4 #include<string.h>
  5 #include<set>
  6 #include<stdio.h>
  7 #include<math.h>
  8 #include<queue>
  9 struct cmp1{
 10     bool operator ()(int &a,int &b){
 11         return a>b;//最小值优先
 12     }
 13 };
 14 struct cmp2{
 15     bool operator ()(int &a,int &b){
 16         return a<b;//最大值优先
 17     }
 18 };
 19 priority_queue<int,vector<int>,cmp1>max1;
 20 priority_queue<int,vector<int>,cmp2>min1;
 21 int main()
 22 {
 23     int t;
 24     int add=0;
 25     cin>>t;
 26     while(t--)
 27     {
 28         while(!max1.empty())
 29             max1.pop();
 30         while(!min1.empty())
 31             min1.pop();
 32         int guanchang,n;
 33         int weizhi=0;
 34         cin>>guanchang>>n;
 35         int flag=1;
 36         int sum=0;
 37         while(n--)
 38         {
 39 
 40             int m;
 41             cin>>m;
 42             if(m==0)
 43             {
 44                 int z;
 45                 cin>>z;
 46                 if(z>=weizhi)
 47                 max1.push(z);
 48                 else if(z<weizhi)
 49                 min1.push(z);
 50             }
 51             else if(m==1)
 52             {
 53                 if(max1.empty()&&min1.empty())
 54                 {
 55                     continue;
 56                 }
 57                 else if(max1.empty())
 58                 {
 59                     sum+=weizhi-min1.top();
 60                     weizhi=min1.top();
 61                     flag=0;
 62                     min1.pop();
 63                     //cout<<"____________"<<endl;
 64                 }
 65                 else if(min1.empty())
 66                 {
 67                     if(max1.top()==weizhi)
 68                     {
 69                         max1.pop();
 70                         continue;
 71                     }
 72                     sum+=max1.top()-weizhi;
 73                     weizhi=max1.top();
 74                     flag=1;
 75                     max1.pop();
 76                     //cout<<"____________"<<flag<<endl;
 77                 }
 78                 else if((max1.top()-weizhi)==(weizhi-min1.top()))
 79                 {
 80                     if(flag)
 81                     {
 82                         sum+=max1.top()-weizhi;
 83                         weizhi=max1.top();
 84                         max1.pop();
 85                     }
 86                     else
 87                     {
 88                         sum+=max1.top()-weizhi;
 89                         weizhi=min1.top();
 90                         min1.pop();
 91                     }
 92                 }
 93 
 94                 else if((max1.top()-weizhi)<(weizhi-min1.top()))
 95                 {
 96                     sum+=max1.top()-weizhi;
 97                     weizhi=max1.top();
 98                     flag=1;
 99                     max1.pop();
100                 }
101                 else if((max1.top()-weizhi)>(weizhi-min1.top()))
102                 {
103                     sum+=weizhi-min1.top();
104                     weizhi=min1.top();
105                     flag=0;
106                     min1.pop();
107                 }
108             }
109         }
110         cout<<"Case "<<++add<<": ";
111         cout<<sum<<endl;
112     }
113     return 0;
114 }
View Code
原文地址:https://www.cnblogs.com/dulute/p/7272599.html