Codeforces Round #268 (Div. 2)

A判断输入的数字有没有覆盖1--n,n最大只有100,所以直接暴力

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
map<int ,int>mmp;
int n;
int main(){
    int i,j,k,b,a;
    while(scanf("%d",&n)!=EOF){
        mmp.clear();
        scanf("%d",&a);
        while(a--){
            scanf("%d",&b);
            mmp[b]=1;
        }
        scanf("%d",&a);
        while(a--){
            scanf("%d",&b);
            mmp[b]=1;
        }
        bool flag=0;
        for(i=1;i<=n;i++){
            if(mmp[i]==0){
                flag=1;
                break;
            }
        }
        if(flag==1){
            printf("Oh, my keyboard!\n");
        }else{
            printf("I become the guy.\n");
        }
    }
    return 0;
}

 

B,如果数据量很大的话可能会用rmq吧,这里直接暴力判定

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
bool vis1[3000];
bool mov[3000];
int main(){
    int p,q,l,r,i,j,k,a,b,c,d;
    while(scanf("%d%d%d%d",&p,&q,&l,&r)!=EOF){
        memset(vis1,0,sizeof(vis1));
        memset(mov,0,sizeof(mov));
        for(i=0;i<p;i++){
            scanf("%d%d",&a,&b);
            for(j=a;j<=b;j++)vis1[j]=1;
        }
        for(i=0;i<q;i++){
            scanf("%d%d",&a,&b);
            for(j=l;j<=r;j++){
                c=a+j;
                d=b+j;
                for(k=c;k<=d;k++){
                    if(vis1[k]){
                        mov[j]=1;
                        break;
                    }
                }
            }
        }
        int res=0;
        for(i=l;i<=r;i++){
            if(mov[i])res++;
        }
        cout<<res<<endl;
    }
    return 0;
}

 

C,这一场最逗比的一题。题意是给出n,用1,2,3,4……n-1,n,这n个数字,只有加减乘算24,为什么逗比呢,看代码就知道了

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
int n;
int main(){
    int i,j,k;
    while(scanf("%d",&n)!=EOF){
        if(n<=3){
            printf("NO\n");
            continue;
        }
        printf("YES\n");
        if(n%2==0){
            printf("3 * 4 = 12\n");
            printf("12 * 2 = 24\n");
            printf("24 * 1 = 24\n");
            for(i=5;i<n;i+=2){
                printf("%d - %d = 1\n",i+1,i);
            }
            for(i=5;i<n;i+=2){
                printf("24 * 1 = 24\n");
            }
        }else{
            printf("5 * 4 = 20\n");
            printf("20 + 2 = 22\n");
            printf("22 + 3 = 25\n");
            printf("25 - 1 = 24\n");
            for(i=6;i<n;i+=2){
                printf("%d - %d = 1\n",i+1,i);
            }
            for(i=6;i<n;i+=2){
                printf("24 * 1 = 24\n");
            }
        }
    }
    return 0;
}

 

D,裸的2-sat,才发现当年的2-sat模板很久没用快发霉了,就当是来验模板吧。这里构图要注意一点:如果第I位数选择a集合时,第J 位数也要选a,同时也要连接一条边第j位选b时第i位也要选b

#include<iostream>
#include<cstdio>
#include <algorithm>
#include<cstring>
#include<map>
using namespace std;
const int inf=1<<31;
const int nMax=300015;
const int mMax=1000010;
class edge{
public:
    int u,v,nex;
};edge e[mMax],e2[mMax];
int k,head[nMax],head2[nMax],k2;//head[i]是以点i为起点的链表头部
int dfn[nMax],low[nMax],sta[nMax],top,atype,ans[nMax],belon[nMax],dep;   //atype 强连通分量的个数


void addedge(int a,int b){//向图中加边的算法,注意加上的是有向边//b为a的后续节点既是a---->b
 //   cout<<a<<" add "<<b<<endl;
    e[k].u=a;
    e[k].v=b;
    e[k].nex=head[a];
    head[a]=k;k++;
}


void addedge2(int a,int b){
 //   cout<<a<<" 222add222 "<<b<<endl;
    e2[k2].u=a;
    e2[k2].v=b;
    e2[k2].nex=head2[a];
    head2[a]=k2;k2++;
}


int tim1[nMax],tim2[nMax],cost[nMax],in[nMax],color[nMax],oppo[nMax],que[nMax];//囧
bool insta[nMax];


void Tarjan(int u){
    int v,i;
    low[u]=dfn[u]=++dep;
    insta[u]=1;
    sta[++top]=u;
    for(i=head[u];i;i=e[i].nex){
        v=e[i].v;
        if(!dfn[v]){
            Tarjan(v);
            if(low[u]>low[v])
                low[u]=low[v];
        }
        else if(insta[v]&&low[u]>dfn[v])
            low[u]=dfn[v];
    }
    if(low[u]==dfn[u]){
        atype++;
        do{
            v=sta[top--];
            insta[v]=false;
            belon[v]=atype;
        }while(u!=v);
    }
    return;
}


void buildremap(){
    int i;
    for(i=1;i<k;i++){
        if(belon[e[i].u]!=belon[e[i].v]){
            addedge2(belon[e[i].v],belon[e[i].u]);
            in[belon[e[i].u]]++;
        }
    }
}

int n,m,a,b,num[100030];
map<int,int>mpp;

void topsort(){
    int i,l=0,r=0,u,v;
    for(i=1;i<=atype;i++){
        if(in[i]==0){
            que[r++]=i;
      //      cout<<"inque "<<i<<endl;
        }
    }
    while(l<r)  {
        u=que[l++];
        if(color[u]==0){
            color[u]=1;
            color[oppo[u]]=-1;
   //         cout<<u<<" u="<<color[u]<<" oppou="<<oppo[u]<<" colorop="<<color[oppo[u]]<<endl;
        }
        v=head2[u];
        while(v!=0){
            if(--in[e2[v].v]==0){
     //           cout<<"inque "<<e2[v].v<<endl;
                que[r++]=e2[v].v;
            }
            v=e2[v].nex;
        }
    }
    for(i=1;i<=n;i++){
        if(color[belon[i*2]]==1)
            ans[i]=1;
    }
}


void init(){
    k=k2=1;
    dep=1;
    top=atype=0;
    memset(ans,0,sizeof(ans));
    memset(color,0,sizeof(color));
    memset(in,0,sizeof(in));
    memset(insta,0,sizeof(insta)); //是否在栈中
    memset(head,0,sizeof(head));   //静态链表头指针
    memset(low,0,sizeof(low));     //Tarjan的low数组
    memset(dfn,0,sizeof(dfn));     //Tarjan的dfn数组
    memset(belon,0,sizeof(belon)); //记录每个点属于哪一个强连通分量
    memset(head2,0,sizeof(head2));
}



bool judge(){
    for(int i=1;i<=n;i++){
        if(belon[i*2]==belon[i*2+1]){
            return 0;
        }
        oppo[belon[i*2]]=belon[i*2+1];
        oppo[belon[i*2+1]]=belon[i*2];
    }
    return 1;
}

int main(){
    int i,tmp;
    while(scanf("%d%d%d",&n,&a,&b)!=EOF){
        mpp.clear();
        for(i=1;i<=n;i++){
            scanf("%d",&num[i]);
            mpp[num[i]]=i;
        }
        //i*2   A
        //i*2+1 B
        init();
        bool flag=1;
        for(i=1;i<=n;i++){
            tmp=num[i];
            if(mpp[a-tmp]==0&&mpp[b-tmp]==0){
                flag=0;
                break;
            }
            if(mpp[a-tmp]!=0&&mpp[b-tmp]==0){
                addedge(i*2+1,i*2);
                addedge(i*2,mpp[a-tmp]*2);
                addedge(mpp[a-tmp]*2+1,i*2+1);
            }
            if(mpp[a-tmp]==0&&mpp[b-tmp]!=0){
                addedge(i*2,i*2+1);
                addedge(i*2+1,mpp[b-tmp]*2+1);
                addedge(mpp[b-tmp]*2,i*2);
            }
            if(mpp[b-tmp]!=0&&mpp[a-tmp]!=0){
                addedge(i*2,mpp[a-tmp]*2);
                addedge(mpp[a-tmp]*2+1,i*2+1);
                addedge(i*2+1,mpp[b-tmp]*2+1);
                addedge(mpp[b-tmp]*2,i*2);
            }
        }
        if(!flag){
            printf("NO\n");
            continue;
        }
        for(i=2;i<=2*n+1;i++){
            if(!dfn[i])Tarjan(i);
        }
        if(judge()){
            printf("YES\n");
            buildremap();
            topsort();
            for(i=1;i<n;i++){
                printf("%d ",1-ans[i]);
            }printf("%d\n",1-ans[n]);
        }
        else printf("NO\n");
    }
    return 0;
}

 

你可能感兴趣的:(codeforces)