codeforces 374D. Inna and Sequence(特别有意思哈)

链接: http://codeforces.com/contest/374/problem/D

题意: 首先给你  n 和 m 然后m 个数,表示位置,这里首先有一个空的序列 然后n 个操作,如果操作是1 那么向序列的末尾加1 ,如果操作是0 ,那么向序列的末尾加0  如果是-1 ,那么删除上边给的m 个位置的数,最后输出序列。为空输出。。。

思路: 因为n m都比较大一开始还真不敢想,然后其实可以发现对于每一次-1 操作其实我是可以暴力每一位置然后二分的,首先我如果前一个位置在序列中已经不存在,那么我可以直接跳出,其次这m个位置各不相同,那么我该位置的L就可以设为以前一个位置找到的对应位置。然后就暴力二分就可以了。复杂度也不会超的。

代码:

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
const int N = 1e6+5;
//  外挂 1

template <class T>
bool scan_d(T &ret)
{
    char c;
    int sgn;
    T bit = 0.1;
    if (c=getchar(), c==EOF)
    {
        return 0;
    }
    while (c!='-'&& c!='.'&& (c<'0'||c>'9'))
    {
        c = getchar();
    }
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0' && c <= '9')
    {
        ret = ret * 10 + (c - '0');
    }
    if (c == ' ' || c == '\n')
    {
        ret *= sgn;
        return 1;
    }
    while (c = getchar(), c >= '0' && c <= '9')
    {
        ret += (c - '0') * bit, bit /= 10;
    }
    ret *= sgn;
    return 1;
}

template <class T>
inline void print_d(T& x)
{
    if (x > 9)
    {
        print_d(x/10);
    }
    putchar(x % 10 + '0');
}

int n,m;
int a[N];
int c[N];
int val[N];
int pos[N];
int vis[N];
int tot=0;

int lowbit(int x)
{
    return x&(-x);
}

void update(int x,int num)
{
    for(;x<=n+2;x+=lowbit(x))
    {
        c[x]+=num;
    }
}

int query(int x)
{
    int sum=0;
    for(;x>0;x-=lowbit(x))
    {
        sum+=c[x];
    }
    return sum;
}

int jud(int x,int aim)
{
    int sum=query(x);
    if(sum>=aim) return 1;
    return 0;
}

int main()
{
    scanf("%d %d",&n,&m);
    for(int i=1;i<=m;i++)
    {
        scan_d(a[i]);
    }
    int op;
    int tmpn=n;
    while(tmpn--)
    {
        scan_d(op);
        if(op==0)
        {
            ++tot;
            update(tot,1);
            val[tot]=0;
        }
        else if(op==1)
        {
            ++tot;
            update(tot,1);
            val[tot]=1;
        }
        else
        {
            int l,r,in,mid;
            int pre=1;

            for(int p=1;p<=m;p++)
            {
                in=-1;
                l=pre; r=tot;
                while(l<=r)
                {
                    mid=(l+r)>>1;
                    if(jud(mid,a[p]))
                    {
                        in=mid;
                        r=mid-1;
                    }
                    else l=mid+1;
                }
                pos[p]=in;
                pre=in;
                if(in==-1) break;
            }

            for(int p=1;p<=m;p++)
            {
                if(pos[p]==-1) break;
                update(pos[p],-1);
                vis[pos[p]]=1;
            }

        }
    }

    int f=0;
    for(int i=1;i<=tot;i++)
    {
        if(vis[i]) continue;
        f=1;
        printf("%d",val[i]);
    }

    if(f==0)
    {
        printf("Poor stack!\n");
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值