标签:des style blog http color java os io strong
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3362 Accepted Submission(s): 1145
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 }
hdu------(4302)Holedox Eating(树状数组+二分)
标签:des style blog http color java os io strong
原文地址:http://www.cnblogs.com/gongxijun/p/3932685.html