Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3362 Accepted Submission(s): 1145
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
Author
BUPT
Source
2012 Multi-University Training Contest 1
代码:
1 #include<cstring>
2 #include<cstdio>
3 #define maxn 100080
4 #define inf 0x3f3f3f3f
5 int next[maxn];
6 int len,n;
7 int lowbit(int x){
8 return x&(-x);
9 }
10 void update(int st,int val){
11 while(st<=len+1){
12 next[st]+=val;
13 st+=lowbit(st);
14 }
15 }
16 int query(int st){
17 int ans=0;
18 while(st>0){
19 ans+=next[st];
20 st-=lowbit(st);
21 }
22 return ans;
23 }
24
25 int main(){
26 int test;
27 int jud,tem,pos,ans;
28 //freopen("test.in","r",stdin);
29 //freopen("test1.in","w",stdout);s
30 scanf("%d",&test);
31 for(int i=1;i<=test;i++ ){
32 scanf("%d%d",&len,&n);
33 memset(next,0,sizeof(next));
34 bool flag=true; //开始从0开始所以必须去右边
35 pos=1; //初始牛的位置在1初开始数
36 ans=0;
37 while(n--)
38 {
39 scanf("%d",&jud);
40 if(!jud){
41 scanf("%d",&tem);
42 update(tem+1,1);
43 }
44 else{ //如果jud=1说明那条牛要吃蛋糕了
45 //但是不知道那边有蛋糕...
46 int left=pos, right=len+1;
47 int mid;
48 int rr=inf; //记录最近的右边蛋糕位置
49 int ll=-inf; //记录最近的左边蛋糕位置
50 bool iseat_r=false;
51 int st_num=query(pos-1); //开始位置以下的位置蛋糕的数目
52 while(left<=right){
53 mid=left+((right-left)>>1);
54 if(query(mid)>st_num) {
55 right=mid-1;
56 rr=mid;
57 iseat_r=true; //表示吃到了蛋糕
58 }
59 else
60 left=mid+1;
61 }
62 left=1;
63 right=pos;
64 st_num=query(pos);
65 bool iseat_l=false;
66 while(left<=right){
67 mid=left+((right-left)>>1);
68 if(st_num-query(mid-1)>0){
69 left=mid+1;
70 ll=mid;
71 iseat_l=true;
72 }
73 else right=mid-1;
74 }
75 if(iseat_l||iseat_r) //又一边有蛋糕吃就可以了,否则什么都不做
76 {
77 int len1=pos-ll;
78 int len2=rr-pos;
79 if(len1<len2){
80 pos=ll;
81 ans+=len1;
82 flag=false;
83 }
84 else { //如果相等,真的可以随便吃吗,以继续保持原来的方向优先
85 if(len1>len2){
86 pos=rr;
87 ans+=len2;
88 flag=true;
89 }
90 else if(flag){ pos=rr;
91 ans+=len2;
92 }
93 else{
94 pos=ll;
95 ans+=len1;
96 }
97 }
98 update(pos,-1); //吃掉了一个蛋糕
99 }
100 }
101 }
102 printf("Case %d: %d\n",i,ans);
103 }
104 return 0;
105 }