zoukankan      html  css  js  c++  java
  • ed div2 D. Bicolored RBS*

    给出n个匹配好的括号  显然n为偶数

    定义:()深度1

    (())深度2  以此类推

    要求 对括号进行01染色   (当然染完色匹配是正确的)

    使得  最大深度最小化

    贪心

    用链式结构   以相邻的()进行轮流01染色直到染完即可

    #include<bits/stdc++.h>
    using namespace std;
    //input by bxd
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i>=(b);--i)
    #define RI(n) scanf("%d",&(n))
    #define RII(n,m) scanf("%d%d",&n,&m)
    #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
    #define RS(s) scanf("%s",s);
    #define ll long long
    #define pb push_back
    #define CLR(A,v)  memset(A,v,sizeof A)
    //////////////////////////////////
    #define inf 0x3f3f3f3f
    const int N=(int)5e5+5;
    char ans[N],s[N];
    int le[N],ri[N];
    
    void del(int x)
    {
        int L=le[x],R=ri[x];
        le[R]=L;
        ri[L]=R;
    }
    
    int main()
    {
        int n;RI(n);RS(s+1);
        
        rep(i,1,n)
        {
            le[i]=i-1;
            ri[i]=i+1;
        }
        ri[0]=1;
        le[n+1]=n;
        
        int cnt=0;
        char t='0';
        while(cnt<n)
        {
            int head=ri[0];
            while(head<=n)
            {
                
                if(s[head]=='('&&s[ri[head]]==')' )
                {   
                    int q=head;int w=ri[head];
                    ans[head]=ans[ri[head]]=t;cnt+=2;
                    head=ri[ri[head]];
                    del(q);del(w);
                }
                else head=ri[head];
            }
            if(t=='0')t='1';
            else t='0';
        }
    
        rep(i,1,n)
        cout<<ans[i];
    
        return 0;
    }
    View Code

    今早重跑居然T了...

    直接递归比较快  

    on

    #include<bits/stdc++.h>
    using namespace std;
    //input by bxd
    #define rep(i,a,b) for(int i=(a);i<=(b);i++)
    #define repp(i,a,b) for(int i=(a);i>=(b);--i)
    #define RI(n) scanf("%d",&(n))
    #define RII(n,m) scanf("%d%d",&n,&m)
    #define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
    #define RS(s) scanf("%s",s);
    #define ll long long
    #define pb push_back
    #define CLR(A,v)  memset(A,v,sizeof A)
    //////////////////////////////////
    #define inf 0x3f3f3f3f
    const int N=2e5+5;
    int ans[N],n,t[N],sta[N],pos;
    char s[N];
    
    void color(int L,int R,int k)
    {
        rep(i,L,R)
            ans[i]=ans[t[i]]=k,color(i+1,t[i]-1,k^1),i=t[i];
    }
    
    int main()
    {
        pos=0;
        RI(n);RS(s+1);
        rep(i,1,n)
            if(s[i]=='(')sta[++pos]=i;
            else
                t[i]=sta[pos],t[sta[pos]]=i,pos--;
    
        color(1,n,0);
        rep(i,1,n)
            printf("%d",ans[i]);
    
    }
    View Code
  • 相关阅读:
    volley框架使用
    Insert Interval
    candy(贪心)
    Best Time to Buy and Sell Stock
    Best Time to Buy and Sell Stock III
    distinct subsequences
    edit distance(编辑距离,两个字符串之间相似性的问题)
    trapping rain water
    word break II(单词切分)
    sudoku solver(数独)
  • 原文地址:https://www.cnblogs.com/bxd123/p/10873183.html
Copyright © 2011-2022 走看看