题解见:http://ecustacm.cn/contest/11/announcements

A

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=2000010;
int a[maxn],ans; char c[maxn];
int main()
{
    int T,N,A,B,C,D,Ca=0;
    scanf("%d",&T);
    while(T--){
        scanf("%s",c+1); N=strlen(c+1);
        A=B=C=D=0;
        rep(i,1,N){
             if(c[i]=='4') A++;
             if(c[i]=='7') B++;
             if(i<N&&c[i]=='4'&&c[i+1]=='7') C++;
             if(i<N&&c[i]=='7'&&c[i+1]=='4') D++;
        }
        printf("Case %d: %d %d %d %d\n",++Ca,A,B,C,D);
    }
    return 0;
}
View Code

B

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=2000010;
char c[maxn];
int main()
{
    int T,A,B,C,D,Ca=0;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d%d%d",&A,&B,&C,&D);
        printf("Case %d: ",++Ca);
        if(A<C||A<D||B<C||B<D||abs(C-D)>1) { puts("-1"); continue;}
        if(B==0){
            rep(i,1,A) putchar('4'); puts("");
        }
        else if(A==0){
            rep(i,1,B) putchar('7'); puts("");
        }
        else if(C==D){
            int ta=C+1,tb=D;
            if(A<ta||B<tb){
                ta=C,tb=C+1;
                if(A<ta||B<tb){ puts("-1"); continue;}
                putchar('7');
                rep(i,1,C){
                    putchar('4');
                    if(i==1){
                        rep(j,1,A-ta) putchar('4');
                    }
                    if(i==C){
                        rep(j,1,B-tb) putchar('7');
                    }
                    putchar('7');
                }
                puts("");
            }
            else {
               rep(i,1,A-ta) putchar('4');
               rep(i,1,C) putchar('4'),putchar('7');
               rep(i,1,B-tb) putchar('7');
               putchar('4');
               puts("");
            }
        }
        else if(C==D+1){
            int ta=C,tb=C;
            if(A<ta||B<tb){ puts("-1"); continue;}
            rep(i,1,A-ta) putchar('4');
            rep(i,1,C) putchar('4'),putchar('7');
            rep(i,1,B-tb) putchar('7');
            puts("");
        }
        else if(D==C+1){
            int ta=D,tb=D;
            if(A<ta||B<tb){ puts("-1"); continue;}
            //cout<<D<<" "<<A-ta<<" "<<B-tb<<endl;
            rep(i,1,D){
                 putchar('7');
                 if(i==1) {
                    rep(j,1,A-ta) putchar('4');
                 }
                 if(i==D) {
                    rep(j,1,B-tb) putchar('7');
                 }
                 putchar('4');
            }
            puts("");
        }
    }
    return 0;
}
View Code

C

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=2000010;
char c[maxn];
struct SAM
{
    int ch[maxn][2],fa[maxn],maxlen[maxn],cnt,last;
    void init()
    {
       cnt=1; last=1; memset(ch[1],0,sizeof(ch[1]));
    }
    void add(int x)
    {
       int np=++cnt,p=last;
       last=np; maxlen[cnt]=maxlen[p]+1;
       memset(ch[np],0,sizeof(ch[np]));
       while(p&&!ch[p][x])ch[p][x]=np,p=fa[p];
       if(!p) fa[np]=1;
       else {
          int q=ch[p][x];
          if(maxlen[q]==maxlen[p]+1) fa[np]=q;
          else {
             int nq=++cnt; maxlen[nq]=maxlen[p]+1;
             memcpy(ch[nq],ch[q],sizeof(ch[q]));
             fa[nq]=fa[q], fa[q]=fa[np]=nq;
             while(p&&ch[p][x]==q) ch[p][x]=nq,p=fa[p];
          }
       }
    }
}S;
int q[maxn],pos[maxn],pre[maxn],head;
int fcy,tail,ans[maxn];
int main()
{
    int T,N,Ca=0;
    scanf("%d",&T);
    while(T--){
        scanf("%d%s",&N,c+1);
        S.init();
        rep(i,1,N) S.add(c[i]=='4'?0:1);
        head=tail=0;
        q[++head]=1; pos[head]=1; pre[head]=0;
        while(tail<head){
            int u=q[++tail];
            q[++head]=S.ch[u][0]; pos[head]=0; pre[head]=tail;
            if(!S.ch[u][0]) {
                fcy=head;
                break;
            }
            q[++head]=S.ch[u][1]; pos[head]=1; pre[head]=tail;
            if(!S.ch[u][1]) {
                fcy=head;
                break;
            }
        }
        int tot=0;
        while(fcy>1){
            ans[++tot]=pos[fcy];
            fcy=pre[fcy];
        }
        printf("Case %d: %d\n",++Ca,tot);
        for(int i=tot;i>=1;i--)
         putchar(ans[i]?'7':'4');
        puts("");
    }
    return 0;
}
View Code

D

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=2000010;
int main()
{
    int T,N,M,Ca=0,ans;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&N,&M);
        ans=max(N,M);
        ans=max(ans,(N*M+1)/2);
        if(N>M) swap(N,M);
        if(N==2) {
            int res=M/4*4;
            if(M%4==1) res+=2;
            if(M%4>=2) res+=4;
            ans=max(ans,res);
        }
        printf("Case %d: %d\n",++Ca,ans);
    }
    return 0;
}
View Code

E

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=1000010;
bitset<maxn>A,B;
int main()
{
    int N,L,R,x,ans; A[0]=1;
    scanf("%d",&N);
    rep(i,1,N){
        scanf("%d%d",&L,&R);
        B.reset();
        rep(j,L,R){
            x=j*j;
            B|=(A<<x);
        }
        A=B;
    }
    ans=A.count();
    printf("%d\n",ans);
    return 0;
}
View Code

F

#include<bits/stdc++.h>
#define rep(i,w,v) for(int i=w;i<=v;i++)
using namespace std;
const int maxn=260;
const int maxm=1000010;
int N,M,K,ans,a[maxn][maxn];
int Laxt[maxn],Next[maxm],To[maxm],cnt;
int linke[maxn],vis[maxn];
void add(int u,int v)
{
    Next[++cnt]=Laxt[u]; Laxt[u]=cnt;To[cnt]=v;
}
bool find(int u)
{
    for(int i=Laxt[u];i;i=Next[i]){
        int v=To[i];if(vis[v]) continue;
        vis[v]=1;
        if(!linke[v]||find(linke[v])){
            linke[v]=u;
            return true;
        }
    }
    return false;
}
bool check(int Mid)
{
    int res=0; cnt=0;
    memset(Laxt,0,sizeof(Laxt));
    memset(linke,0,sizeof(linke));
    rep(i,1,N) rep(j,1,M)
      if(a[i][j]<=Mid) add(i,j);
    rep(i,1,N){
        memset(vis,0,sizeof(vis));
        if(find(i)) res++;
    }
    return res>=K;
}
int main()
{
    scanf("%d%d%d",&N,&M,&K); K=N+1-K;
    rep(i,1,N) rep(j,1,M) scanf("%d",&a[i][j]);
    int L=1,R=1000000000;
    while(L<=R){
        int Mid=(L+R)>>1;
        if(check(Mid)) ans=Mid,R=Mid-1;
        else L=Mid+1;
    }
    printf("%d\n",ans);
    return 0;
}
View Code