CODE FESTIVAL 2017 qual B solution to a problem

Portal

\(A\)

Gugu Gu

const int N=55;
char s[N];int n;
int main(){
    scanf("%s",s+1),n=strlen(s+1);
    s[n-7]='\0',printf("%s\n",s+1);
    return 0;
}

\(B\)

Gugu Gu

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
multiset<int>s;int n,m;
typedef multiset<int>::iterator IT;
int main(){
    scanf("%d",&n);
    for(R int i=1,x;i<=n;++i)scanf("%d",&x),s.insert(x);
    IT it;
    scanf("%d",&m);
    for(R int i=1,x;i<=m;++i){
        scanf("%d",&x);
        it=s.lower_bound(x);
        if(it==s.end()||(*it)!=x)return puts("NO"),0;
        s.erase(it);
    }
    puts("YES");
    return 0;
}

\(C\)

If the distance between two points \ (D \) , then every connected easily apparent to one side so that the intermediate \ (D = D +. 1. 3-\) , so when the two points have sides in the final figures and only if their distance is an odd number. So if there is an odd ring final figure can be turned into a complete graph, otherwise it can be connected to each other side between black and white coloring, black and white dot dot

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
typedef long long ll;
const int N=1e5+5;
struct eg{int v,nx;}e[N<<1];int head[N],tot;
inline void add(R int u,R int v){e[++tot]={v,head[u]},head[u]=tot;}
ll res;int cnt[2],dis[N],deg[N],n,m,fl;
void dfs(int u,int d){
    dis[u]=d,++cnt[d];
    go(u)if(dis[v]==-1)dfs(v,d^1);
        else if(dis[v]!=d^1)fl=1;
}
int main(){
    scanf("%d%d",&n,&m);
    for(R int i=1,u,v;i<=m;++i){
        scanf("%d%d",&u,&v),add(u,v),add(v,u);
        ++deg[u],++deg[v];
    }
    memset(dis,-1,(n+1)<<2);
    dfs(1,0);
    if(fl)return printf("%lld\n",(1ll*n*(n-1)>>1)-m),0;
    fp(i,1,n)res+=cnt[dis[i]^1]-deg[i];
    printf("%lld\n",res>>1);
    return 0;
}

\(D\)

Consider the final sequence, a \ (1 \) must be from the beginning of the \ (11,111 ... 101 \) or \ (101 ... 111 \) transferred from, and assuming \ (K \) th \ (1 \) , then the contribution of that \ (K-1 \) , we denote the strings so as to form a good, the string length is the sum of all the good string \ (O (n) \) , then the pre- deal with it directly \ (dp \) on the line

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int N=5e5+5;
char s[N];int las[N],f[N],n;
int main(){
    scanf("%d%s",&n,s+1);
    fp(i,1,n)las[i]=(s[i]=='1'?las[i-1]:i);
    fp(i,1,n){
        f[i]=f[i-1];
        if(s[i]=='1'&&las[i]&&s[las[i]-1]=='1'){
            R int x=las[i],y=las[x-1];
            cmax(f[i],f[x-2]+i-x);
            if(i==x+1)fp(j,y,x-2)cmax(f[i],f[j]+x-1-j);
        }
    }
    printf("%d\n",f[n]);
    return 0;
}

\(E\)

First found in the left side of the red ball basketball forever, and if we need to choose a red ball, must have left a \ (s \) or \ (t \) at the beginning into the red ball is optimal

Then we enumerate to \ (Snuke \) number of beginning of the continuous ball basketball then set \ (g_ {i, j} \) represents a left \ (I \) th and basketball \ (J \ ) red balls, and to \ (Snuke \) at the beginning of the sequence number must be the first red ball of program

Consider \ (G \) , so \ (t \) have to be placed at the beginning of the red ball, that \ (i + 1 \) in this position, then chose a total of \ (j \) after the ball, the \ (t \) no use

Consider finished choose \ (j \) after a ball, still found a similar question, but this time only a \ (s \) can put

So we define \ (e_ {i, j} \) represents \ (I \) Blue \ (J \) red, must red open, \ (F_ {I, J} \) represents \ (I \) Blue \ (j \) red, there is a \ (s \) can be put, when transferred, for \ (e \) ago enumeration \ (j \) th in the number of blue for \ (f \) to pieces the number of blue at the beginning of the move, then transfer equation is

\[ \begin{aligned} &e_{i,j}=\sum_{k\leq i}{j-1\choose k}\\ &f_{i,j}=\sum_{k\leq i}e_{i-k,j}\\ &g_{i,j}=\sum_{k\leq i}{j-1\choose k}f_{i-k,k}\\ &h_{i,j}=\sum_{k\leq i}g_{i-k,j}\\ \end{aligned} \]

\ (e, f \) of the prefix and the transfer may optimize away, but \ (G \) does not work. We found that we will eventually require the answer is \ (n-H_ {,} m \) , it is only necessary to consider those \ (j = m \) a \ (G \) it

Thus complexity \ (O (n ^ 2) \)

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
const int P=1e9+7;
inline void upd(R int &x,R int y){(x+=y)>=P?x-=P:0;}
inline int add(R int x,R int y){return x+y>=P?x+y-P:x+y;}
inline int dec(R int x,R int y){return x-y<0?x-y+P:x-y;}
inline int mul(R int x,R int y){return 1ll*x*y-1ll*x*y/P*P;}
int ksm(R int x,R int y){
    R int res=1;
    for(;y;y>>=1,x=mul(x,x))(y&1)?res=mul(res,x):0;
    return res;
}
const int N=2005;
int e[N][N],f[N][N],g[N][N],h[N][N],cs[N][N],sb[N][N],n,m;
void init(){
    fp(i,0,max(n,m)){
        cs[i][0]=1;
        fp(j,1,i)cs[i][j]=add(cs[i-1][j],cs[i-1][j-1]);
        sb[i][0]=1;fp(j,1,n)sb[i][j]=add(sb[i][j-1],cs[i][j]);
    }
    fp(i,0,n)fp(j,1,m)e[i][j]=sb[j-1][i];
    e[0][0]=1;
    fp(i,1,n)fp(j,0,m)upd(e[i][j],e[i-1][j]);
    fp(i,0,n)fp(j,0,m)f[i][j]=e[i][j];
    fp(i,0,n)fp(k,0,i)upd(g[i][m],mul(cs[m-1][k],f[i-k][k]));
    g[0][0]=1;
    fp(k,0,n)upd(h[n][m],g[n-k][m]);
}
int main(){
//  freopen("testdata.in","r",stdin);
    scanf("%d%d",&n,&m);
    init();
    printf("%d\n",h[n][m]);
    return 0;
}

\(F\)

Every time the current minimum and maximum substring splicing sequence, then the new string into the set until the next set up a left element

//quming
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
struct node{
    string s;int c;
    inline node(R string ss,R int cc):s(ss),c(cc){}
    inline bool operator <(const node &b)const{return s<b.s;}
};set<node>s;
int x,y,z;
int main(){
    scanf("%d%d%d",&x,&y,&z);
    if(x)s.insert(node("a",x));
    if(y)s.insert(node("b",y));
    if(z)s.insert(node("c",z));
    while(2333){
        if(s.size()==1){
            node p=*s.begin();
            fp(i,1,p.c)cout<<p.s;
            puts("");
            return 0;
        }
        R node p=*s.begin(),q=*--s.end();
        s.erase(s.begin()),s.erase(--s.end());
        R int k=min(p.c,q.c);s.insert(node(p.s+q.s,k));
        if(p.c-k)s.insert(node(p.s,p.c-k));
        if(q.c-k)s.insert(node(q.s,q.c-k));
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/yuanquming/p/11619103.html