模拟赛总结。

 

第一题:田忌赛马

    算法(Algorithm):贪心

    注意(Attention):None

    难度(Difficulty):Easy NOIP_PJ_T1_T2

第二题:01迷宫

    算法(Algorithm):FloodFill

    注意(Attention):DFS会爆栈

    难度(Difficulty):Middle NOIP_PJ_T2_T3

第三题:喝茶时间

    算法(Algorithm):并查集

    注意(Attention):初值问题

    难度(Difficulty):Easy NOIP_PJ_T2

第四题:奶牛飞盘队

    算法(Algorithm):DP背包

    注意(Attention):读入时要将R_i对F取模来优化程序

    难度(Difficulty):Middle NOIP_PJ_T4

==============================田忌赛马=================================

#include<stdio.h>
#include<algorithm>
using namespace std;
const int maxn=101;
int a[maxn],b[maxn],N,ans;
inline bool cmp(int a,int b){return a<b;}
int main(){
    scanf("%d",&N);int ts,ks,tf,kf;ts=ks=N;tf=kf=1;
    for(int i=1;i<=N;i++)scanf("%d",&a[i]);
    for(int i=1;i<=N;i++)scanf("%d",&b[i]);
    sort(a+1,a+1+N,cmp);
    sort(b+1,b+1+N,cmp);
    while(ts>=tf)
      if(a[ts]<b[ks])ts--,ks--,ans++;
      else if(a[ts]>b[ks])
             if(a[ts]>b[kf])ts--,kf++;
             else if(a[ts]<b[kf])ans++,ts--,kf++;
                  else ts--,kf++;
           else
             if(a[tf]<b[kf])ans++,kf++,tf++;
             else if(a[tf]>b[kf])ts--,kf++;
                  else
                    if(a[ts]>b[kf])ts--,kf++;
                    else ts--,kf++;
     printf("%d\n",ans);
     return 0;
}


 

==========================01迷宫=============================

#include<stdio.h>
#include<queue>
using namespace std;
const int mx[]={0,0,0,1,-1};
const int my[]={0,1,-1,0,0};
const int maxn=1001;
struct node{int x,y;bool z;};
queue <struct node> Q;
int color[maxn][maxn],sum[maxn*maxn],N,M,now;
bool m[maxn][maxn];
void bfs(int x,int y){
     struct node tmp;tmp.x=x;tmp.y=y;tmp.z=m[x][y];
     Q.push(tmp);color[x][y]=now;
     while(!Q.empty()){
       int x=Q.front().x,y=Q.front().y;bool z=Q.front().z;
       for(int i=1;i<=4;i++){
         int nx=x+mx[i],ny=y+my[i];
         if(nx<=0||nx>N||ny<=0||ny>N)continue;
         if(color[nx][ny]||m[nx][ny]==z)continue;
         color[nx][ny]=now;sum[now]++;
         tmp.x=nx;tmp.y=ny;tmp.z=m[nx][ny];
         Q.push(tmp);
       }
       Q.pop();
     }
}
int main(){
    scanf("%d",&N);scanf("%d",&M);
    for(int i=1;i<=N;i++){
      getchar();
      for(int j=1;j<=N;j++)m[i][j]=getchar()-'0';
    }
    for(int i=1;i<=N;i++)
      for(int j=1;j<=N;j++)
        if(color[i][j]==0)now++,color[i][j]=now,sum[now]++,bfs(i,j);
    for(int i=1,x,y;i<=M;i++){
      scanf("%d",&x);scanf("%d",&y);
      printf("%d\n",sum[color[x][y]]);
    }
    return 0;
}


 

===============================喝茶时间=============================

#include<stdio.h>
#include<string.h>
const int maxn=1001;
int root[maxn],N,M,Q;
int find_root(int a){int r=a,s;while(r!=root[r])r=root[r];s=a;while(s!=root[s])a=s,s=root[s],root[a]=r;return r;}
void merge(int ra,int rb){root[rb]=ra;}
int main(){
    scanf("%d",&N);scanf("%d",&M);scanf("%d",&Q);
    for(int i=1;i<=N;i++)root[i]=i;
    for(int i=1,x,y;i<=M;i++){
      scanf("%d",&x);scanf("%d",&y);
      merge(find_root(x),find_root(y));
    }
    for(int i=1,x,y;i<=Q;i++){
      scanf("%d",&x);scanf("%d",&y);
      if(find_root(x)==find_root(y))printf("Y\n");
      else printf("N\n");
    }
    return 0;
}


 

============================奶牛飞盘队==================================

#include<stdio.h>
#include<string.h>
const int mod=100000000;
const int maxn=2001;
int a[maxn],b[maxn],tmp[maxn],N,F;
int main(){
    scanf("%d",&N);scanf("%d",&F);
    for(int i=1;i<=N;i++)scanf("%d",&a[i]),a[i]%=F;
    for(int i=1;i<=N;i++){
      memset(tmp,0,sizeof(tmp));
      for(int j=0;j<F;j++)tmp[(j+a[i])%F]=(b[(j+a[i])%F]+b[j])%mod;
      for(int j=0;j<F;j++)b[j]=tmp[j];
      b[a[i]]++;b[a[i]]%=mod;
    }
    printf("%d\n",b[0]);
    return 0;
}


 

 ============================End=========================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值