Educational Codeforces Round 78

Posted hgangang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Educational Codeforces Round 78相关的知识,希望对你有一定的参考价值。

D

https://www.luogu.com.cn/problem/CF1278D

扫描线判断构成的边是否n-1条,用并查集寻父亲判断会不会构成环

#include<bits/stdc++.h>
using namespace std;
const int maxn=5e5+10;
set<int>s;
int n,l,r,res,fa[maxn],cnt,R[maxn],book,mp[maxn*2];
struct node{
    int pos,id,op;
}A[maxn*2];

bool cmp(node a,node b){return a.pos<b.pos;}
int getfa(int x){return fa[x]==x?x:fa[x]=getfa(fa[x]);}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        scanf("%d%d",&l,&r);
        A[++cnt].op=1,A[cnt].pos=l,A[cnt].id=i;
        A[++cnt].op=0,A[cnt].pos=r,A[cnt].id=i;
        R[i]=r;
        mp[r]=i;
        mp[l]=i;
    }
    sort(A+1,A+1+cnt,cmp);
    for(int i=1;i<=n;i++) fa[i]=i;
//    for(int i=1;i<=cnt;i++) cout<<A[i].pos<<‘ ‘;
    for(int i=1;i<=cnt;i++){
        if(A[i].op==1){
            for(auto it=s.begin();it!=s.end();it++){
                if(*it>R[A[i].id]) break;
                res++;if(res>=n) break;
                int u=getfa(A[i].id),v=getfa(mp[*it]);
//                cout<<"U="<<u<<‘ ‘<<v<<endl;
                if(u==v) book=1;else fa[u]=v;
             }
             s.insert(R[A[i].id]);
        }
        else {s.erase(s.find(A[i].pos));}
    }
    if(res!=n-1) cout<<"NO"<<endl;
    else {
        if(book) cout<<"NO"<<endl;
        else cout<<"YES"<<endl;
    }
}

  C

2n的1 2序列,你在n和n+1之间,你可以往左边往右边吃过去,你需要吃最少的1 2,使得剩余的1 2数量相等。

把2改为-1,这样就可以通过前缀后缀来找得最优值

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map> 
using namespace std;
const int N=2e5+10;
const int inf=0x3f3f3f3f;
int main(){
    int t;
    cin>>t;
    while(t--){
        int s[N]={0};
        map<int,int> pos;
        int i;
        int n;
        cin>>n;
        pos[0]=0;
        for(i=1;i<=2*n;i++){
            int a;
            cin>>a;
            if(a==2)
            a=-1;
            s[i]=s[i-1]+a;
            if(i<=n)
            pos[s[i]]=i;
        }
        int res=2*n;
        for(i=n;i<=2*n;i++){
            auto it=pos.find(s[i]-s[2*n]);//(-(s[2*n]-s[i])表示的是后缀的负值)
            if(it!=pos.end())
            res=min(res,i-it->second);
        }
        cout<<res<<endl;
    }
}

  

以上是关于Educational Codeforces Round 78的主要内容,如果未能解决你的问题,请参考以下文章

Educational Codeforces Round 7 A

Educational Codeforces Round 7

Educational Codeforces Round 90

Educational Codeforces Round 33

Codeforces Educational Codeforces Round 54 题解

Educational Codeforces Round 27