Educational Codeforces Round 78

D

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

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

#include
using namespace std;
const int maxn=5e5+10;
sets;
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.posR[A[i].id]) break;
                res++;if(res>=n) break;
                int u=getfa(A[i].id),v=getfa(mp[*it]);
//                cout<<"U="< 
 

  C

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

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

#include
#include
#include
#include
#include
#include 
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 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< 
 

  

你可能感兴趣的:(Educational Codeforces Round 78)