hdu 4302 Holedox Eating(线段树或MAP,SET,4级)

Holedox Eating

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2341    Accepted Submission(s): 792


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
 

Recommend
zhuyuanchen520

思路:模拟就行了,如果是用线段树,那就找0->pos的最大值和pos->L的最小值,线段树存插入区间断最大值和最小值。


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define lson t<<1
#define rson t<<1|1
#define mid (l+r)/2
using namespace std;
const int oo=1e9;
const int mm=1e5+9;
class node
{
 public:int mi,ma,l,r,num;
}rt[mm*4];
int L,n;
void build(int t,int l,int r)
{
 rt[t].l=l;rt[t].r=r;rt[t].num=0;rt[t].mi=oo;rt[t].ma=-oo;
 if(l==r){return;}
 build(lson,l,mid);build(rson,mid+1,r);
}
void update(int t,int id,bool flag)
{
 if(rt[t].l==rt[t].r&&rt[t].l==id)
 {
 if(flag){rt[t].num++;rt[t].mi=rt[t].ma=id;}
 else {rt[t].num--;if(!rt[t].num)rt[t].ma=-oo,rt[t].mi=oo;}
 return;
 }
 if(rt[lson].r>=id)update(lson,id,flag);
 else update(rson,id,flag);
 rt[t].num=rt[lson].num+rt[rson].num;
 rt[t].ma=max(rt[lson].ma,rt[rson].ma);
 rt[t].mi=min(rt[lson].mi,rt[rson].mi);
}
int query(int t,int l,int r,bool flag)
{
  if(l<=rt[t].l&&rt[t].r<=r)
  {
   if(flag)return rt[t].ma;
   else return rt[t].mi;
  }
  int ret;
  if(flag)ret=-oo;
  else ret=oo;
  if(l<=rt[lson].r)
  {if(flag)ret=max(ret,query(lson,l,r,flag));
   else ret=min(ret,query(lson,l,r,flag));
  }
  if(r>=rt[rson].l)
  {if(flag)ret=max(ret,query(rson,l,r,flag));
   else ret=min(ret,query(rson,l,r,flag));
  }
  return ret;
}
int main()
{
 int cas,a,b,num,ans;bool dir;
 while(~scanf("%d",&cas))
 {for(int ca=1;ca<=cas;++ca)
  {
  scanf("%d%d",&L,&n);
 /// puts("yes");
  build(1,0,L);
  dir=1;int pos=0;ans=num=0;
  for(int i=0;i<n;++i)
  {
   scanf("%d",&a);
   if(!a)
   {
   scanf("%d",&b);update(1,b,1);++num;
   }
   else
   { if(!num)continue;--num;
    int ll=query(1,0,pos,1);
    int rr=query(1,pos,L,0);
    ///cout<<ll<<" "<<rr<<" "<<ans<<endl;
    if(ll==-oo){ans+=rr-pos;update(1,rr,0);pos=rr;dir=1;continue;}
    if(rr==oo){ans+=pos-ll;update(1,ll,0);pos=ll;dir=0;continue;}
    a=pos-ll;b=rr-pos;
    if(a<b){ans+=a;update(1,ll,0);pos=ll;dir=0;continue;}
    else if(a>b){ans+=b;update(1,rr,0);pos=rr;dir=1;continue;}
    else if(a==b)
    {
      if(dir){ans+=b;update(1,rr,0);pos=rr;dir=1;continue;}
      else {ans+=a;update(1,ll,0);pos=ll;dir=0;continue;}
    }
   }
  }
  printf("Case %d: %d\n",ca,ans);
  }
 }
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
using namespace std;
map<int,int>mp;
const int oo=1e9;
bool dir;
map<int,int>::iterator it,il,ir;
int main()
{
  int cas,l,n,a,b,num;
    while(~scanf("%d",&cas))
    {for(int ca=1;ca<=cas;++ca)
     {mp.clear();mp[oo]=1;mp[-oo]=1;
     num=0;mp[0]=1;
     it=il=ir=mp.find(0);
     int ans=0;dir=1;
     scanf("%d%d",&l,&n);
     for(int i=0;i<n;++i)
     {
       scanf("%d",&a);
       if(!a)
       {++num;
       scanf("%d",&a);mp[a]++;
       }
       else
       { if(!num)continue;--num;
       il=ir=it;
       --il;++ir;
        int z=it->first;
         --mp[z];///去除z
         if(mp[z]){continue;}
         else mp.erase(it);
         if(il->first==-oo){ans+=ir->first-it->first;it=ir;dir=1;continue;}
         if(ir->first==oo){ans+=it->first-il->first;it=il;dir=0;continue;}

         a=it->first-il->first;b=ir->first-it->first;
         if(a<b){ans+=a;it=il;dir=0;continue;}
         else if(a>b){ans+=b;it=ir;dir=1;continue;}
         else if(a==b)
         {if(dir){ans+=b;it=ir;}
          else {ans+=a;it=il;}
         }
       }
     }
     printf("Case %d: %d\n",ca,ans);

     }
    }
}



posted @ 2013-05-23 19:09  剑不飞  阅读(254)  评论(0编辑  收藏  举报