• Codeforces Round #220 (Div. 2)D. Inna and Sequence 树状数组+二分


    题目链接:D. Inna and Sequence

    题意:三种操作,1往序列后面加个1,0往序列后面加个0,-1,把给定位置上的数删除。

    题解:用树状数组保存的数没被删的个数,每次二分找到位置,然后用数组标记这里被删除。

    #include<bits/stdc++.h>
    #include<set>
    #include<iostream>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #define pb push_back
    #define ll long long
    #define PI 3.14159265
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    #define eps 1e-7
    typedef unsigned long long ull;
    const int mod=1e9+9;
    const int inf=0x3f3f3f3f;
    const int maxn=1e6+5;
    const int root=1e6+7;
    using namespace std;
    ll t,n,m,k,len;
    int tot,ed;
    int a[maxn],b[maxn],v[maxn];
    int p[maxn];
    vector<int>st;
    int  lowbit(int x)
    {
        return x&(-x);
    }
    void add(int x,int y)
    {
        for(int i=x;i<maxn;i+=lowbit(i))
        {
            b[i]+=y;
        }
    }
    int get_sum(int x)
    {
        int ans=0;
        while(x>0)
        {
            ans+=b[x];
            x-=lowbit(x);
        }
        return ans;
    }
    int sec(int x)
    {
        int l=1,r=ed;
        while(l<=r)
        {
            int mid=(l+r)>>1;
            if(get_sum(mid)>=x)r=mid-1;
            else l=mid+1;
        }
        return l;
    }
    void view()
    {
         for(int i=1;i<=ed;i++)
        {
            if(!v[i])cout<<a[i];
        }
        cout<<endl;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cin>>n>>m;
        for(int i=0;i<m;i++)
        {
            cin>>p[i];
        }
        int op;
        while(n--)
        {
            cin>>op;
            if(op==1)
            {
                ++tot;
                a[++ed]=1;
                add(ed,1);
            }
            else if(op==0)
            {
                ++tot;
                ++ed;
                add(ed,1);
            }
            else
            {
                if(tot<=0)continue;
                int pp=lower_bound(p,p+m,tot)-p;
                if(p[pp]==tot)pp++;
                for(int i=0;i<pp;i++)
                {
                    int l=sec(p[i]);
                    st.pb(l);
                    tot--;
                    v[l]=true;
                }
                for(int i=0;i<st.size();i++)
                {
                    add(st[i],-1);
                }
                st.clear();
            }
         //   view();
        }
        if(tot>0)
        {
            view();
        }
        else{
            cout<<"Poor stack!"<<endl;
        }
        return 0;
    }
  • 相关阅读:
    好学习法
    error execution phase upload-config/kubelet: Error writing Crisocket information for the control-plane node: timed out waiting for the condition
    linux 大实话
    解决vi/vim中粘贴 格式错乱
    centos7 调试系统服务命令
    gitlab jenkins docker kubernetes
    修改centos7主机名
    野战ci/cd
    相互交流生成快捷网页链接
    设置centos7静态网卡配置文件
  • 原文地址:https://www.cnblogs.com/lhclqslove/p/8424180.html
Copyright © 2020-2023  润新知