note: This article comes from the Internet. Please contact me via lethic@163.com if there is any infringement.
lethic@163.com.

Holedox Eating
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1187 Accepted Submission(s): 391

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 dont 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 multiset

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<stack>
#include<algorithm>
 
using namespace std;
 
const int N=100005;
struct node
{
    int l,r;
    int sum;
}mem[N*3];
 
int num[N];//
int place,suml,sumr;//  
int to;// 1  0 
int ans;//
int L,R;//
int Search(int,int );
void insert(int ,int ,int );
void Right()// 
{
    to=1;
    sumr-=num[R];
    insert(1,R,-num[R]);
    --num[R];
    ans=ans+R-place;
    place=R;
}
void Left()// 
{
    to=0;
    suml-=num[L];
    insert(1,L,-num[L]);
    --num[L];
    ans=ans+place-L;
    place=L;
}
void build(int x,int i,int j)//
{
    mem[x].l=i;
    mem[x].r=j;
    mem[x].sum=0;
    if(i==j)
    return ;
    int mid=(i+j)>>1;
    build(x*2,i,mid);
    build(x*2+1,mid+1,j);
}
void insert(int x,int p,int k)//p k k 
{
    int mid=(mem[x].l+mem[x].r)>>1;
    if(mem[x].l==mem[x].r)
    {
        mem[x].sum+=k;
        return ;
    }
    if(p<=mid)
    insert(x*2,p,k);
    else
    insert(x*2+1,p,k);
    mem[x].sum=mem[x*2].sum+mem[x*2+1].sum;
}
int Search(int x,int d)//d
{
    if(mem[x].l==mem[x].r)
    return mem[x].r;
    if(mem[x*2].sum>=d)
    return Search(x*2,d);
    else
    return Search(x*2+1,d-mem[x*2].sum);
}
int main()
{
   int T;
   scanf("%d",&T);
   for(int w=1;w<=T;++w)
   {
       int n,m;
       place=0,suml=0,sumr=0;
       to=1;
       ans=0;
       scanf("%d %d",&n,&m);
       build(1,0,n);
       memset(num,0,sizeof(num));
       while(m--)
       {
           int k,x;
           scanf("%d",&k);
           if(k==0)
           {
               scanf("%d",&x);
               ++num[x];
               if(x!=place)// 
               insert(1,x,1);
               if(x<place)
               ++suml;
               else if(x>place)
               ++sumr;
           }else
           {
               if(num[place]>0)//  
               {
                   --num[place];
                   continue;
               }
               if(suml==0&&sumr==0)//
               continue;
               if(sumr==0)// 
               {
                   L=Search(1,suml);
                   Left();
               }else
               if(suml==0)//
               {
                   R=Search(1,1);
                   Right();
               }else
               if(suml>0&&sumr>0)
               {
                   L=Search(1,suml);//
                   R=Search(1,suml+1);//
                   if(place-L<R-place||(place-L==R-place&&to==0))
                   Left();
                   else
                   Right();
               }
           }
       }
       printf("Case %d: %d\n",w,ans);
   }
   return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
priority_queue<int,vector<int>,greater<int> > minQue;
priority_queue<int> maxQue;
int main(){
	long long res;
	int k,l,n,left,targ,val,right,curr,dir;
	while(scanf("%d",&k)!=EOF){
		for(int i=1;i<=k;i++){
			while(!minQue.empty()) minQue.pop();
			while(!maxQue.empty()) maxQue.pop();
			res=0;
			curr=0;
			dir=1;
			scanf("%d%d",&l,&n);
			while(n--){
				scanf("%d",&targ);
				if(targ){
					if(minQue.empty()){
						if(!maxQue.empty()){
							left=maxQue.top();
							maxQue.pop();
							res+=curr-left;
							if(curr!=left) dir=0;
							curr=left;
						}
					}
					else{
						if(maxQue.empty()){
							right=minQue.top();
							minQue.pop();
							res+=right-curr;
							curr=right;
							dir=1;
						}
						else{
							left=maxQue.top();
							right=minQue.top();
							if(curr-left<right-curr){
								res+=curr-left;
								if(curr!=left) dir=0;
								curr=left;
								maxQue.pop();
							}
							else if(curr-left>right-curr){
								res+=right-curr;
								dir=1;
								curr=right;
								minQue.pop();
							}
							else{
								if(dir){
									res+=right-curr;
									dir=1;
									curr=right;
									minQue.pop();
								}
								else{
									res+=curr-left;
									if(curr!=left) dir=0;
									curr=left;
									maxQue.pop();
								}
							}
						}
					}
				}
				else{
					scanf("%d",&val);
					if(val<=curr) maxQue.push(val);
					else minQue.push(val);
				}
			}
			printf("Case %d: ",i);
			cout<<res<<endl;
		}
	}
	return 0;
}

multiset:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include<iostream>
#include<cstring>
#include<cstdio>
#include<set>
#include<cmath>
using namespace std;
int main(){
	int t;
	long long sum;
	int L,n;
	int a,b;
	long long Min,p;
	scanf("%d",&t);
	for(int Case=1;Case<=t;Case++){
		scanf("%d%d",&L,&n);
		sum=0;
		multiset<int>s;                       //multisetset
		multiset<int>::iterator It,del;
		int cur=0,pre=0;
		while(n--){
			 scanf("%d",&a);
			 if(a==0){
				 scanf("%d",&b);
				 s.insert(b);
			 }
			 else{
				  Min=0x3fffffff;
				  if(!s.size())
					  continue;
				  for(It=s.begin();It!=s.end();It++){
					  if(abs(*It-cur)<Min){   
						  Min=abs((*It)-cur);
						  p=(*It);           //
						  del=It;            //
					  }
					  else if(abs(*It-cur)==Min){   //
						  if((*It-cur)*(cur-pre)>0){
							  Min=abs((*It)-cur);
							  p=(*It);
							  del=It;
						  }
					  }
				  }
				  s.erase(del);
				  sum+=Min;
				  pre=cur;                    //
				  cur=p;                      //
			 }
		}
		printf("Case %d: %I64d\n",Case,sum);
	}
//	system("pause");
	return 0;
}