• CodeForces 374D Inna and Sequence


    #include<bits/stdc++.h>
    using namespace std;
    #define for(i,a,b) for(int i=a;i<=b;++i)
    
    const int maxn=1e6+5;
    int w[maxn];
    int ftree[maxn];
    int a[maxn];
    int tmp[maxn];
    int n,m;
    
    int lowbit(int x)
    {
    	return x&(-x);
    }
    int sum(int i)
    {
    	int s=0;
    	while(i>0)
    	{
    		s+=ftree[i];
    		i-=lowbit(i);
    	}
    	return s;
    }
    void add(int i,int x)
    {
    	while(i<=n)
    	{
    		ftree[i]+=x;
    		i+=lowbit(i);
    	}
    }
    int ask(int i)
    {
    	return sum(i)-sum(i-1);
    }
    int lookfor(int x,int n)
    {
    	if(x>sum(n))return -1;
    	int l,m,r;
    	l=1,r=n;
    	while(l<r)
    	{
    		m=(l+r)/2;
    		if(sum(m)<x)l=m+1;
    		else r=m;
    	}
    	return l;
    }
    int main()
    {
    	while(~scanf("%d%d",&n,&m))//each case
    	{
    		memset(ftree,0,sizeof ftree);
    		for(i,1,m)scanf("%d",a+i);
    		int op;
    		int now=0;
    		for(i,1,n)//i of n operation
    		{
    			scanf("%d",&op);
    			if(op<0)
    			{
    				vector<int> tmp;
    				for(j,1,m)//j of m deleting array
    				{
    					int kk=lookfor(a[j],now);
    					if(kk<0)break;
    					else tmp.push_back(kk);
    				}
    				int kk=tmp.size();
    				for(i,0,kk-1)add(tmp[i],-1);
    			}
    			else
    			{
    				w[++now]=op;
    				add(now,1);
    			}
    		}
    		int flag=0;
    		for(i,1,now)
    			if(ask(i)==1)
    			{
    				flag=1;
    				printf("%d",w[i]);
    			}
    		if(flag==0)printf("Poor stack!");
    		printf("
    ");
    	}
    }
    
  • 相关阅读:
    php apc
    nginx https
    js弹出确认框,挺全
    websocket nodejs
    nodejs express测试
    【C++】Mandelbrot集绘制(生成ppm文件)
    【Scheme】Huffman树
    【Scheme】符号求导
    【Scheme】树结构
    【Scheme】序列的操作
  • 原文地址:https://www.cnblogs.com/maoruimas/p/9585645.html
Copyright © 2020-2023  润新知