先上题目:
Holedox Eating
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2793 Accepted Submission(s): 919
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.
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
题意是在一条直线上一开始一只小动物在0点,然后有两种操作,一种是在某一点上放一块蛋糕,另一种是去吃一块蛋糕。其中吃蛋糕有规定,先吃离小动物最近的蛋糕,如果有两块蛋糕离小动物的距离一样近,先吃当时的朝向的蛋糕,问所有操作以后小动物所走的路多长。
这题又看就是模拟题,没有什么太难搞的地方,当然做法就是记录当前小动物的左边和右边最近的那一块蛋糕的位置,还有小动物的朝向,然后就直接模拟就可以了。当然,这里我为了方便用了优先队列来保存当前位置的左右两边的蛋糕位置。
上代码:
1 #include <stdio.h> 2 #include <string.h> 3 #include <queue> 4 #include <math.h> 5 #include <algorithm> 6 #define MAX (100000+10) 7 using namespace std; 8 9 long long sum; 10 11 struct cmp1 12 { 13 bool operator() (int x,int y) 14 { 15 return x<y; 16 } 17 }; 18 19 struct cmp2 20 { 21 bool operator() (int x,int y) 22 { 23 return x>y; 24 } 25 }; 26 27 priority_queue<int,vector<int>,cmp1> L; 28 priority_queue<int,vector<int>,cmp2> R; 29 30 int main() 31 { 32 int t,j,n,c,w,m,r; 33 bool f; 34 //freopen("data.txt","r",stdin); 35 scanf("%d ",&t); 36 for(j=1; j<=t; j++) 37 { 38 while(!L.empty()) L.pop(); 39 while(!R.empty()) R.pop(); 40 sum=0; 41 m=0; 42 f=1; 43 scanf("%d %d",&r,&n); 44 while(n--) 45 { 46 scanf("%d",&c); 47 if(c) 48 { 49 if(L.empty() && R.empty()) continue; 50 if(L.empty()) 51 { sum+=R.top()-m; m=R.top(); R.pop(); f=1; } 52 else if(R.empty()) 53 { sum+=m-L.top(); m=L.top(); L.pop(); f=0; } 54 else if(m-L.top()<R.top()-m) 55 { sum+=m-L.top(); m=L.top(); L.pop(); f=0; } 56 else if(R.top()-m<m-L.top()) 57 { sum+=R.top()-m; m=R.top(); R.pop(); f=1; } 58 else if(f) 59 { sum+=R.top()-m; m=R.top(); R.pop(); f=1; } 60 else 61 { sum+=m-L.top(); m=L.top(); L.pop(); f=0; } 62 } 63 else 64 { 65 scanf("%d",&w); 66 if(w<m) L.push(w); 67 else R.push(w); 68 } 69 } 70 printf("Case %d: %lld ",j,sum); 71 } 72 return 0; 73 }