Inna and Sequence
生活随笔
收集整理的這篇文章主要介紹了
Inna and Sequence
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Codeforces Round #220 (Div. 2)?D:http://codeforces.com/contest/374/problem/D
題意:給你m個數,這m個數是遞增的。然后給你n個操作,每個操作是一個數1,0,-1,如果是1或者0,就把這個數數直接放在序列的末位,剛開始的時候,序列為空。當操作數是-1的時候,只要把ai<=當期序列長度,就把ai所對應的那一位刪除。
題解:用樹狀數組和二分搞。首先如果是0或者1,直接把這個數放進去,把這個數的位子加一。然后更新的時候,首先查詢到小于等于序列長度的那些數,表示要刪除在該序列的第幾個數,然后用二分找到這個數,但是這里不能直接刪除,要先保存,然后等所有的數都找完了,再進行統(tǒng)一刪除。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 const int N=1e6+10; 7 int c[N],a[N],ans[N],top,as[N]; 8 int n,m,sum; 9 void init(){ 10 memset(c,0,sizeof(c)); 11 memset(a,0,sizeof(a)); 12 memset(ans,-1,sizeof(ans)); 13 top=0; 14 } 15 int lowbit(int x){ 16 return x&(-x); 17 18 } 19 void add(int x,int val){ 20 while(x<=m){ 21 c[x]+=val; 22 x+=lowbit(x); 23 } 24 } 25 26 int getsum(int x){ 27 int ans=0; 28 while(x){ 29 ans+=c[x]; 30 x-=lowbit(x); 31 } 32 return ans; 33 } 34 void work(int num,int ct){ 35 int l=1,r=m; 36 while(l<r){ 37 int mid=(l+r)/2; 38 //printf("%d\n",mid); 39 if(getsum(mid)>=num)r=mid; 40 else 41 l=mid+1; 42 } 43 as[ct]=l; 44 } 45 int t; 46 int main(){ 47 while(~scanf("%d%d",&m,&n)){ 48 init(); 49 for(int i=1;i<=n;i++){ 50 scanf("%d",&a[i]); 51 } 52 for(int i=1;i<=m;i++){ 53 scanf("%d",&t); 54 if(t==0||t==1){ 55 ans[++top]=t; 56 sum++; 57 add(top,1); 58 } 59 else{ 60 int temp=lower_bound(a+1,a+n+1,sum)-a; 61 if(temp>n)temp--; 62 else if(a[temp]>sum)temp--; 63 sum-=temp; 64 // printf("%d\n",temp); 65 for(int i=1;i<=temp;i++) 66 work(a[i],i); 67 for(int i=1;i<=temp;i++){ 68 add(as[i],-1); 69 ans[as[i]]=-1; 70 } 71 } 72 73 } 74 bool flag=false; 75 for(int i=1;i<=m;i++){ 76 if(ans[i]==-1)continue; 77 printf("%d",ans[i]); 78 flag=true; 79 } 80 if(!flag)printf("Poor stack!"); 81 puts(""); 82 83 } 84 } View Code?
轉載于:https://www.cnblogs.com/chujian123/p/3892151.html
總結
以上是生活随笔為你收集整理的Inna and Sequence的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【UIKit】UITableView.0
- 下一篇: SurfaceViewDemo