1. 程式人生 > >2017-10-13 NOIP模擬賽

2017-10-13 NOIP模擬賽

color style http gen 9.png noi image push front

技術分享

入陣曲

技術分享

技術分享

技術分享

技術分享
#include<iostream>
#include<cstdio>
#define maxn 401
#ifdef WIN32
#define PLL "%I64d"
#else
#define PLL "%lld"
#endif
using namespace std;
int n,m,K,a[maxn][maxn],sum[maxn][maxn];
bool flag=1;
long long ans;
int main(){
    //freopen("Cola.txt","r",stdin);
freopen("rally.in","r",stdin);freopen("rally.out","w",stdout); scanf("%d%d%d",&n,&m,&K); int w; for(int i=1;i<=n;i++) for(int j=1;j<=m;j++){ scanf("%d",&a[i][j]); sum[i][j]=a[i][j]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
if(i==1&&j==1)w=a[i][j]; else if(w!=a[i][j])flag=0; } if(flag){ for(int i=1;i=n;i++) for(int j=1;j<=m;j++) if(sum[i][j]%K==0)ans+=1LL*(n-i+1)*(m-j+1); printf(PLL,ans); return 0; } for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) for(int k=i;k<=n;k++) for(int l=j;l<=m;l++) if((sum[k][l]-sum[i-1][l]-sum[k][j-1]+sum[i-1][j-1])%K==0)ans++; printf(PLL,ans); return 0; }
50分 前綴和暴力

將軍令

技術分享

技術分享

技術分享

技術分享

技術分享
#include<iostream>
#include<cstdio>
#define maxn 100010
#define INF 0x7fffffff
using namespace std;
int n,k,t,num,head[maxn],f[maxn][5],dep[maxn],mdep[maxn];
struct node{
    int to,pre;
}e[maxn*2];
void Insert(int from,int to){
    e[++num].to=to;
    e[num].pre=head[from];
    head[from]=num;
}
void dfs(int now,int father){
    mdep[now]=dep[now]=dep[father]+1;
    for(int i=head[now];i;i=e[i].pre){
        int to=e[i].to;
        if(to==father)continue;
        dfs(to,now);
        mdep[now]=max(mdep[now],dep[to]);
    }
}
void dfs2(int now,int father){
    int f0=0,f1=0,f2=0;bool fl=0;
    for(int i=head[now];i;i=e[i].pre){
        int to=e[i].to;
        if(to==father)continue;
        fl=1;
        dfs2(to,now);
        f0+=min(f[to][0],min(f[to][1],f[to][2]));
        f1+=f[to][0];
        f2+=f[to][1];
    }
    if(!fl){
        f[now][0]=1;f[now][1]=0,f[now][2]=0;
        return;
    }
    f[now][0]=f0+1;f[now][1]=f1;f[now][2]=max(1,f2);
}
void dfs3(int now,int father){
    int f0=0,f1=0,f2=0,f3=0;bool fl=0;
    for(int i=head[now];i;i=e[i].pre){
        int to=e[i].to;
        if(to==father)continue;
        fl=1;
        dfs3(to,now);
        f0+=min(f[to][0],min(f[to][1],min(f[to][2],f[to][3])));
        f1+=f[to][0];
        f2+=f[to][1];
        f3+=f[to][2];
    }
    if(!fl){
        f[now][0]=1;f[now][1]=0;f[now][2]=0;f[now][3]=0;
        return;
    }
    f[now][0]=f0+1;f[now][1]=f1;f[now][2]=f2;f[now][3]=f3;
    if(mdep[now]-dep[now]>=k)f[now][3]=max(1,f[now][3]);
}
void dfs4(int now,int father){
    int f0=0,f1=0,f2=0,f3=0,f4=0;bool fl=0;
    for(int i=head[now];i;i=e[i].pre){
        int to=e[i].to;
        if(to==father)continue;
        fl=1;
        dfs4(to,now);
        f0+=min(f[to][0],min(f[to][1],min(f[to][2],min(f[to][3],f[to][4]))));
        f1+=f[to][0];
        f2+=f[to][1];
        f3+=f[to][2];
        f4+=f[to][3];
    }
    if(!fl){
        f[now][0]=1;f[now][1]=0;f[now][2]=0;f[now][3]=0;f[now][4]=0;
        return;
    }
    f[now][0]=f0+1;f[now][1]=f1;f[now][2]=f2;f[now][3]=f3;f[now][4]=f4;
    if(mdep[now]-dep[now]>=k)f[now][4]=max(1,f[now][4]);
}
int main(){
    //freopen("Cola.txt","r",stdin);
    freopen("general.in","r",stdin);freopen("general.out","w",stdout);
    scanf("%d%d%d",&n,&k,&t);
    int x,y;
    for(int i=1;i<n;i++){
        scanf("%d%d",&x,&y);
        Insert(x,y);Insert(y,x);
    }
    if(k==0){
        printf("%d",n);
        return 0;
    }
    dfs(1,0);
    if(k==1){
        dfs2(1,0);
        printf("%d",max(1,min(f[1][0],f[1][1])));
        return 0;
    }
    if(k==2){
        dfs3(1,0);
        printf("%d",max(1,min(f[1][0],min(f[1][1],f[1][2]))));
        return 0;
    }
    if(k==3){
        dfs4(1,0);
        printf("%d",max(1,min(f[1][0],min(f[1][1],min(f[1][2],f[1][3])))));
        return 0;
    }
    printf("%d",(n/(k+2)+1));
    return 0;
}
60分 3個樹形dp解決部分數據

星空

技術分享

技術分享

技術分享

技術分享
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
int s,n,k,m,op[65],opsz[65];
struct node{
    int sta,step;
};
node make_node(int x,int y){
    node res;
    res.sta=x,res.step=y;
    return res;
}
queue<node>q;
bool vis[1<<25];
int main(){
    //freopen("Soda.txt","r",stdin);
    freopen("starlit.in","r",stdin);freopen("starlit.out","w",stdout);
    scanf("%d%d%d",&n,&k,&m);
    s=(1<<n)-1;
    int end=(1<<n)-1;
    int x;
    for(int i=1;i<=k;i++){
        scanf("%d",&x);
        s=s^(1<<(x-1));
    }
    for(int i=1;i<=m;i++){
        scanf("%d",&x);
        op[i]=(1<<x)-1;
        opsz[i]=x;
    }
    vis[s]=1;
    q.push(make_node(s,0));
    while(!q.empty()){
        node cur=q.front();q.pop();
        int stanow=cur.sta;
        for(int i=1;i<=m;i++){
            for(int j=0;j<=n-opsz[i];j++){
                int now=stanow^(op[i]<<j);
                if(now==end){
                    printf("%d",cur.step+1);
                    return 0;
                }
                if(!vis[now]){
                    vis[now]=1;
                    q.push(make_node(now,cur.step+1));
                }
            }
        }
    }
    return 0;
}
24分 前6個點,狀態壓縮+暴力

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

技術分享

2017-10-13 NOIP模擬賽