hdu------(4302)Holedox Eating(樹狀數組+二分)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   java   os   io   strong   

Holedox Eating

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


Problem DescriptionHoledox 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. 

 

InputThe 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. 

 

OutputOutput the total distance Holedox will move. Holedox don’t need to return to the position 0. 

 

Sample Input310 80 10 510 20 0111 10 70 10 510 20 01110 80 10 10 510 20 011 

 

Sample OutputCase 1: 9Case 2: 4Case 3: 2 

 

AuthorBUPT 

 

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 }
View Code

 

hdu------(4302)Holedox Eating(樹狀數組+二分)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.