Holedox Eating
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 241 Accepted Submission(s): 107
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
Source
Recommend
zhuyuanchen520
刚学会树状数组就临危受命做树状数组的题,鸭梨颇大……
二分想不清楚。。orz……
代码
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string.h>
using namespace std;
#define MAXN 100005
#define INF 999999999
int in[MAXN];
int n;
int lowbit(int t)
{
return t&(-t);
}
void Add(int t,int val)
{
while (t<=n)
{
in[t]+=val;
t+=lowbit(t);
}
}
int Query(int t)
{
if (t==0) return 0;
int sum=0;
while(t>0)
{
sum+=in[t];
t-=lowbit(t);
}
return sum;
}
int Search1(int t)
{
int l,r,mid;
l=t;
r=n;
while(l<=r)
{
mid=(l+r)/2;
if (Query(mid)-Query(t-1)>0) r=mid-1;
else l=mid+1;
}
if (Query(l)-Query(l-1)==0) return INF;
return l;
}
int Search2(int t)
{
int l,r,mid;
l=1;
r=t;
while(1)
{
if (l>r) break;
mid=(l+r)/2;
if (Query(t)-Query(mid-1)>0) l=mid+1;
else r=mid-1;
}
if (Query(r)==0) return INF;
return r;
}
int main()
{
int i,j,T,k,tmp,dir,x,y,cnt=1;
long long s;
scanf("%d",&T);
while(T--)
{
memset(in,0,sizeof(in));
scanf("%d%d",&n,&k);
n++;
tmp=1;
dir=1;
s=0;
while(k--)
{
scanf("%d",&x);
if (x==0)
{
scanf("%d",&y);
y++;
Add(y,1);
}
else
{
x=Search1(tmp);
y=Search2(tmp);
if (x==INF && y==INF) continue;
if (tmp-y>x-tmp || y==INF)
{
dir=1;
s+=x-tmp;
tmp=x;
Add(x,-1);
}
else if (tmp-y<x-tmp || x==INF)
{
dir=-1;
s+=tmp-y;
tmp=y;
Add(y,-1);
}
else if (dir==1)
{
s+=x-tmp;
tmp=x;
Add(x,-1);
}
else
{
s+=tmp-y;
tmp=y;
Add(y,-1);
}
}
}
printf("Case %d: %I64d\n",cnt++,s);
}
return 0;
}