zoukankan      html  css  js  c++  java
  • Codeforces Round #656 (Div. 3)

    A、Three Pairwise Maximums

    https://codeforces.com/contest/1385/problem/A

    题目大意:输入x,y,z,其中x,y,z,是max(a,b),max(a,c),max(b,c),求a,b,c

    题解:假设a是最大的,则max(a,b)=a,max(a,c)=a。则x,y,z中必有两个一样的数且最大,剩下的那个数设为1就好。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    
    int T;
    
    
    int main()
    {
        cin>>T;
        while(T--)
        {
            int a[4];
            cin>>a[1]>>a[2]>>a[3];
            sort(a+1,a+3+1);
            if(a[3]!=a[2])
            {
                cout<<"NO
    ";
                continue;
            }    
            cout<<"YES
    "; 
            cout<<a[3]<<" "<<a[1]<<" "<<"1
    ";
        }
        return 0;
    } 

    B.Restore the Permutation by Merger

    https://codeforces.com/contest/1385/problem/B

    题目大意:两个大小均为n的相同排列按在原序列中的顺序插在一起,求原排列。

    题解:从左到右扫,没输出过的输出就可以。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    
    int T,n;
    
    int vis[55];
    
    int main()
    {
        cin>>T;
        while(T--)
        {
            cin>>n;
            n=n*2;
            int x;
            memset(vis,0,sizeof(vis));
            for(int i=1;i<=n;i++) 
            {
                cin>>x;
                if(!vis[x])
                {
                    cout<<x<<" ";
                    vis[x]=true;
                }
            }
            cout<<endl;
        }
        return 0;
    } 

    C. Make It Good

    https://codeforces.com/contest/1385/problem/C

    题目大意:好序列的定义:每次从一个序列的头或者尾取数字组成的新序列单调不减。问一个序列最少删减多少前缀能成为好序列。

    题解:发现好序列:单调不减或单调不增或单峰。从右往左遍历。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define N 200009
    using namespace std;
    
    int T;
    
    int n;
    int a[N];
    
    int main()
    {
        cin>>T;
        while(T--)
        {
            cin>>n;
            for(int i=1;i<=n;i++) cin>>a[i];
            int now=n;
            for(int i=n;i>=1;i--) 
            {
                if(a[i]<=a[i-1]) now--;
                else break; 
            }
            int tmp=now;
            for(int i=tmp;i>=1;i--)
            {
                if(a[i]>=a[i-1]) now--;
                else break;
            }
            if(now)now--;
            cout<<now<<endl;
        }
        return 0;
    } 

    D. a-Good String

    https://codeforces.com/contest/1385/problem/D

    题目大意:讲不明白。。。

    题解:搜索(我晕 没算好时间复杂度 没敢写搜索)O(nlogn)

    代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define N 131077
    using namespace std;
    
    int T;
    
    int n,len,ans;
    
    char s[N];
    
    int a[30][N];
    
    //12345678
    
    void slove(int l,int r,int tp,int now)
    {
        if(l==r)
        {
            if(s[l]-'a'+1!=tp) now++;
            ans=min(ans,now);
            return ; 
        } 
        int mid=(l+r)>>1;
        slove(mid+1,r,tp+1,now+(mid-l+1)-(a[tp][mid]-a[tp][l-1]));
        slove(l,mid,tp+1,now+(r-mid)-(a[tp][r]-a[tp][mid])); 
    }
    
    int main()
    {
        cin>>T;
        while(T--)
        {
            cin>>n;
            scanf("%s",s+1);
            len=strlen(s+1);ans=len;
            for(int i=1;i<=len;i++)
            {
                for(int j=1;j<=26;j++)
                {
                    a[j][i]=a[j][i-1];
                }
                a[s[i]-'a'+1][i]++; 
            }    
            slove(1,len,1,0);
            cout<<ans<<endl;
        }
        return 0;
    }

    E. Directing Edges

    https://codeforces.com/contest/1385/problem/E

    题目大意:给定一个图,有的边有向,有的边无向,让你自己规定无向边的方向,使这个图为有向无环图。

    题解:拓扑排序判断有无环+拓扑序小的为出边所在的点(这样才能保证拓扑序不变) 有向边加入图中,无向边单独处理。

    代码:

    #include<iostream>
    #include<cstdio>
    #include<queue>
    #include<cstring>
    #include<algorithm>
    #define N 200009 
    using namespace std;
    queue<int>q;
    
    int T,js;
    bool flag;
    int n,m;
    int sumedge;
    
    int head[N],xx[N],yy[N],vis[N],col[N],In_du[N],dep[N];
    
    struct Edge
    {
        int x,y,nxt;
        Edge(int x=0,int y=0,int nxt=0):
        x(x),y(y),nxt(nxt){} 
    }edge[N];
    
    void add(int x,int y)
    {
        edge[++sumedge]=Edge(x,y,head[x]);
        head[x]=sumedge;
    }
    
    void Clear_Init()
    {
        memset(In_du,0,sizeof(In_du));
        memset(head,0,sizeof(head));
        while(!q.empty()) q.pop();
        sumedge=0;flag=true;js=0;
        cin>>n>>m;
        for(int i=1;i<=m;i++)
        {
            int p,x,y;
            cin>>p>>x>>y;
            if(p==1)
            {
                add(x,y);
                In_du[y]++;
            }
            xx[i]=x,yy[i]=y;
        }
    }
    
    bool DFS(int x)
    {
        vis[x]=true;
        col[x]=0;
        for(int i=head[x];i;i=edge[i].nxt)
        {
            int v=edge[i].y;
            if(col[v]==0) return false;
            else if(col[v]==-1&&!DFS(v)) return false;
        }
        col[x]=1;
        return true;
    }
    
    void pre_slove()
    {
        for(int i=1;i<=n;i++)
        {
            if(!vis[i]&&!DFS(i))
            {
                cout<<"NO
    ";
                flag=false;
                return ;
            }
        }
    }
    
    void Top_sort()
    {
        for(int i=1;i<=n;i++)
        {
            if(!In_du[i]) q.push(i); 
        }
        while(!q.empty())
        {
            int now=q.front();q.pop();
            dep[now]=++js; 
            for(int i=head[now];i;i=edge[i].nxt)
            {
                int v=edge[i].y;
                In_du[v]--;
                if(!In_du[v]) q.push(v); 
            }
        }
    }
    
    void Put()
    {
        cout<<"YES
    ";
        for(int i=1;i<=m;i++)
        {
            int x=xx[i],y=yy[i];
            if(dep[x]<dep[y]) cout<<x<<" "<<y<<endl;
            else cout<<y<<" "<<x<<endl;
        }
    }
    
    int main()
    {
        cin>>T;
        while(T--)
        {
            Clear_Init();
            //pre_slove();
            //if(!flag) continue;
            Top_sort();
            if(js<n) cout<<"NO
    ";
            else Put();
        }
        return 0;
    }

    剩下的题解会有的。一定。QWQ

  • 相关阅读:
    UE4 Abc 批量导入
    UE4源码摘录(424)
    JZ10 矩形覆盖
    JZ27 字符串的排列
    JZ66 机器人的运动范围
    JZ65 矩阵中的路径
    JZ12 数值的整数次方
    JZ37 数字在升序数组中出现的次数
    JZ6 旋转数组的最小数字
    JZ67 剪绳子
  • 原文地址:https://www.cnblogs.com/zzyh/p/13375185.html
Copyright © 2011-2022 走看看