标签:ios ups log string begin sam bsp with desc
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3864 Accepted Submission(s): 1715
1 //求大于a的第k个数就是求第sum(a)+k小的数。二分求第k小数。 2 #include<iostream> 3 #include<cstdio> 4 #include<cstring> 5 using namespace std; 6 int A[100005]; 7 int m; 8 int lowbit(int x) 9 { 10 return x&(-x); 11 } 12 void add(int id,int val) 13 { 14 while(id<=100000) 15 { 16 A[id]+=val; 17 id+=lowbit(id); 18 } 19 } 20 int sum(int id) 21 { 22 int s=0; 23 while(id>0) 24 { 25 s+=A[id]; 26 id-=lowbit(id); 27 } 28 return s; 29 } 30 int main() 31 { 32 int a,b,p; 33 while(scanf("%d",&m)!=EOF) 34 { 35 memset(A,0,sizeof(A)); 36 for(int i=0;i<m;i++) 37 { 38 scanf("%d",&p); 39 if(p==0) 40 { 41 scanf("%d",&a); 42 add(a,1); 43 } 44 else if(p==1) 45 { 46 scanf("%d",&a); 47 if(sum(a)-sum(a-1)==0) 48 printf("No Elment!\n"); 49 else add(a,-1); 50 } 51 else if(p==2) 52 { 53 scanf("%d%d",&a,&b); 54 int tem=sum(a); 55 if(sum(100000)-tem<b) 56 { 57 printf("Not Find!\n"); 58 continue; 59 } 60 tem+=b; 61 int lef=1,rig=100000,mid; 62 while(lef+1<rig) //二分结束条件!!!!!!! 63 { 64 mid=(lef+rig)>>1; 65 int num=sum(mid); 66 if(num>=tem) rig=mid; 67 else lef=mid; 68 } 69 printf("%d\n",rig); 70 } 71 } 72 } 73 return 0; 74 }
标签:ios ups log string begin sam bsp with desc
原文地址:http://www.cnblogs.com/--ZHIYUAN/p/6042353.html