• 2017.5.27 NOIP模拟赛(hzwer2014-5-16 NOIP模拟赛)


    期望得分:100+100+60+30=290

    实际得分:100+20+60+0=180

     当务之急:提高一次正确率

    Problem 1 双色球(ball.cpp/c/pas)

    【题目描述】

        机房来了新一届的学弟学妹,邪恶的chenzeyu97发现一位学弟与他同名,于是他当起了善良的学长233

    “来来来,学弟,我考你道水题检验一下你的水平……”

    一个栈内初始有n个红色和蓝色的小球,请你按照以下规则进行操作

    1. 只要栈顶的小球是红色的,将其取出,直到栈顶的球是蓝色
    2. 然后将栈顶的蓝球变成红色
    3. 最后放入若干个蓝球直到栈中的球数为n

    以上3步骤为一次操作

    如栈中都是红色球,则操作停止,请问几次操作后停止

    chenzeyu97出完题发现他自己不能AC所以想请你帮忙

    【输入格式】

    第一行为一个整数n,表示栈的容量为n

    第二行为一个字符串,第i个字符表示自顶向下的第i个球的颜色,R代表红色,B代表蓝色

    【输出格式】

    一个整数表示操作数

    【样例输入】

    样例1:

    3

    RBR

    样例2:

    4

    RBBR

    【样例输出】

    样例1:2

    样例2:6

    【数据范围】

    50%的数据,1<=n<=20

    100%的数据,1<=n<=50

     

    自栈顶往下第i个蓝色球,把它变为红色球需要的次数为2^(i-1)

    #include<cstdio>
    #include<iostream>
    using namespace std;
    long long ans;
    char ch[150];
    int main()
    {
        int n;
        scanf("%d",&n);
        scanf("%s",ch);
        long long j=1;
        for(int i=0;i<n;i++,j*=2)
         if(ch[i]=='B') ans+=j;
        cout<<ans;
    }
    View Code

    Problem 2 魔方(cube.cpp/c/pas)

    【题目描述】

    ccy(ndsf)觉得手动复原魔方太慢了,所以他要借助计算机。

    ccy(ndsf)家的魔方都是3*3*3的三阶魔方,大家应该都见过。



    3的“顺时针”改为“逆时针”,即3 4以图为准。)
    ccy(ndfs)从网上搜了一篇攻略,并找人翻译成了他自己会做的方法。现在告诉你他的魔方情况,以及他从网上搜到的攻略,请你求出最后魔方变成什么样子。

    【输入格式】
       第一行,一串数字,表示从网上搜到的攻略。
       下面6*3行,每行3个数字,每三行表示魔方一个面的情况,六个面的顺序是前、后、左、右、上、下。

    【输出格式】
       6*3行,表示处理后的魔方,形式同输入。

    【样例输入】

    23
    121
    221
    111
    123
    321
    111
    123
    321
    132
    132
    231
    132
    121
    112
    233
    332
    111
    333

    【样例输出】

    123
    222
    113
    212
    321
    113
    122
    321
    132
    121
    333
    121
    211
    312
    113
    331
    111
    331

    【数据范围】

    40%的数据,攻略的长度小于5且仅有4种操作的其中一种

    100%的数据,攻略的长度小于100

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    char ch[150];
    string f[4],b[4],l[4],r[4],u[4],d[4],tmp,tmp2[4];
    int main()
    {
        scanf("%s",ch+1);
        int len=strlen(ch+1);
        for(int i=1;i<=3;i++) 
        {
            cin>>tmp;
            f[i]='0'+tmp;
        }
        for(int i=1;i<=3;i++) 
        {
            cin>>tmp;
            b[i]='0'+tmp;
        }
        for(int i=1;i<=3;i++) 
        {
            cin>>tmp;
            l[i]='0'+tmp;
        }
        for(int i=1;i<=3;i++) 
        {
            cin>>tmp;
            r[i]='0'+tmp;
        }
        for(int i=1;i<=3;i++) 
        {
            cin>>tmp;
            u[i]='0'+tmp;
        }
        for(int i=1;i<=3;i++) 
        {
            cin>>tmp;
            d[i]='0'+tmp;
        }
        int j;
        for(int i=1;i<=len;i++)
        {
            if(ch[i]=='2')
            {
                for(j=1;j<=3;j++) tmp[j]=f[j][3];
                for(j=1;j<=3;j++) f[j][3]=u[j][3];
                for(j=1;j<=3;j++) u[j][3]=b[j][3];
                for(j=1;j<=3;j++) b[j][3]=d[j][3];
                for(j=1;j<=3;j++) d[j][3]=tmp[j];
                for(j=1;j<=3;j++) tmp2[j]=r[j];
                for(int l=1;l<=3;l++)
                 for(int h=1;h<=3;h++)
                  r[h][l]=tmp2[l][3-h+1];
            }
            else if(ch[i]=='1')
            {
                for(j=1;j<=3;j++) tmp[j]=f[j][3];
                for(j=1;j<=3;j++) f[j][3]=d[j][3];
                for(j=1;j<=3;j++) d[j][3]=b[j][3];
                for(j=1;j<=3;j++) b[j][3]=u[j][3];
                for(j=1;j<=3;j++) u[j][3]=tmp[j];
                for(j=1;j<=3;j++) tmp2[j]=r[j];
                for(int h=1;h<=3;h++) 
                 for(int l=1;l<=3;l++)
                  r[h][l]=tmp2[3-l+1][h];
            }
            else if(ch[i]=='3')
            {
                tmp=f[1];
                f[1]=l[1];
                l[1]=b[1];
                b[1]=r[1];
                r[1]=tmp;
                for(j=1;j<=3;j++) tmp2[j]=u[j];
                for(int h=1;h<=3;h++)
                 for(int l=1;l<=3;l++)
                  u[h][l]=tmp2[3-l+1][h];
            }
            else
            {
                tmp=f[1];
                f[1]=r[1];
                r[1]=b[1];
                b[1]=l[1];
                l[1]=tmp;
                for(j=1;j<=3;j++) tmp2[j]=u[j];
                for(int l=1;l<=3;l++)
                 for(int h=1;h<=3;h++)
                  u[h][l]=tmp2[l][3-h+1];
            }
        }
        int k;
        for(j=1;j<=3;j++) 
        {
            for(k=1;k<=3;k++) 
              cout<<f[j][k];
            cout<<endl;
        }
        for(j=1;j<=3;j++) 
        {
            for(k=1;k<=3;k++) 
              cout<<b[j][k];
            cout<<endl;
        }
        for(j=1;j<=3;j++) 
        {
            for(k=1;k<=3;k++) 
              cout<<l[j][k];
            cout<<endl;
        }
        for(j=1;j<=3;j++) 
        {
            for(k=1;k<=3;k++) 
              cout<<r[j][k];
            cout<<endl;
        }
        for(j=1;j<=3;j++) 
        {
            for(k=1;k<=3;k++) 
              cout<<u[j][k];
            cout<<endl;
        }
        for(j=1;j<=3;j++) 
        {
            for(k=1;k<=3;k++) 
              cout<<d[j][k];
            cout<<endl;
        }
    }
    View Code

    第二次愚蠢的错误:复制的时候没有修改

    Problem 3 czy的后宫(harem.cpp/c/pas)

    【题目描述】

    czy要妥善安排他的后宫,他想在机房摆一群妹子,一共有n个位置排成一排,每个位置可以摆妹子也可以不摆妹子。有些类型妹子如果摆在相邻的位置(隔着一个空的位置不算相邻),就不好看了。假定每种妹子数量无限,求摆妹子的方案数。

    【输入格式】

    输入有m+1行,第一行有两个用空格隔开的正整数n、m,m表示妹子的种类数。接下来的m行,每行有m个字符1或0,若第i行第j列为1,则表示第i种妹子第j种妹子不能排在相邻的位置,输入保证对称。(提示:同一种妹子可能不能排在相邻位置)。

    【输出格式】

    输出只有一个整数,为方案数(这个数字可能很大,请输出方案数除以1000000007的余数。

    【样例输入】

    2 2

    01

    10

    【样例输出】

    7

    【样例说明】

    七种方案为(空,空)、(空,1)、(1、空)、(2、空)、(空、2)、(1,1)、(2,2)。

    【数据范围】

    20%的数据,1<n≤5,0<m≤10。

    60%的数据,1<n≤200,0<m≤100。

    100%的数据,1<n≤1000000000,0<m≤100。

    注:此题时限1.5s是因为本评测机跑太慢,大家正常做

    但写的太丑可能T一俩个点

    60分思路:

    f[i][j]表示前i个位置,最后一个位置为第j类的方案数

    状态转移:如果j与k能挨在一起 f[i][j]+=+f[i-1][k]

    矩阵乘法优化可以满分

    系数矩阵就是题目给的01矩阵(0、1反过来)

    注意,空位置视作0,0和所有的妹子都可以挨在一起

    AC代码:

    #include<cstdio>
    #define mod 1000000007
    using namespace std;
    long long v[111][111],tmp[111][111],a[111][111];
    char ch[111][111];
    int n,m;
    long long ans;
    void mul(long long s1[111][111],long long s2[111][111])
    {
        for(int i=0;i<=m;i++)
         for(int j=0;j<=m;j++)
          for(int k=0;k<=m;k++)
           tmp[i][j]=(tmp[i][j]+s1[i][k]*s2[k][j])%mod;
        for(int i=0;i<=m;i++)
         for(int j=0;j<=m;j++)
          s1[i][j]=tmp[i][j],tmp[i][j]=0;
    }
    int main()
    {
        freopen("harem.in","r",stdin);
        freopen("harem.out","w",stdout);
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
          scanf("%s",ch[i]+1);
        for(int i=1;i<=m;i++)
         for(int j=1;j<=m;j++)
          if(ch[i][j]=='0') v[i][j]=a[i][j]=1;
        for(int i=0;i<=m;i++) v[0][i]=v[i][0]=a[i][0]=a[0][i]=1;
        n--;
        for(;n;n>>=1,mul(v,v))
         if(n&1) mul(a,v);
        for(int i=0;i<=m;i++) ans=(ans+a[i][0])%mod;
        printf("%d",ans);
    }
    View Code

    60分代码:

    #include<cstdio>
    #define mod 1000000007
    using namespace std;
    int f[511][501];
    bool v[501][501];
    char ch[501][501];
    int main()
    {
        freopen("harem.in","r",stdin);
        freopen("harem.out","w",stdout);
        int n,m;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=m;i++)
          scanf("%s",ch[i]+1);
        for(int i=1;i<=m;i++)
         for(int j=1;j<=m;j++)
          if(ch[i][j]=='1') v[i][j]=1;
        f[0][0]=1;
        for(int i=1;i<=n;i++)
         for(int j=0;j<=m;j++)
          {
               for(int k=0;k<=m;k++)
                if(!v[j][k]) f[i][j]=(f[i][j]+f[i-1][k])%mod;
          }
        int ans=0;
        for(int i=0;i<=m;i++) ans=(ans+f[n][i])%mod;
        printf("%d",ans);
    }
    View Code

    Problem 4 mex(mex.cpp/c/pas)

    【题目描述】

     

    【输入格式】

     

    【输出格式】

     

    【样例输入】

    7 5

    0 2 1 0 1 3 2

    1 3

    2 3

    1 4

    3 6

    2 7

    【样例输出】

    3

    0

    3

    2

    4

    【样例解释与数据范围】

     

     法一,莫队算法:

    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    #define N 200001
    using namespace std;
    int n,a[N],siz,sum[N],an[N],ans;
    struct node
    {
        int l,r,bl,id;
    }e[N];
    inline bool cmp(node p,node q)
    {
        if(p.bl!=q.bl) return p.bl<q.bl;
        return p.r<q.r;
    }
    inline void updata(int pos,int w,int g)
    {
        sum[a[pos]]+=w;
        if(g==1&&a[pos]==ans)  while(sum[ans]) ans++;  
        if(g==2&&a[pos]<ans&&!sum[a[pos]])  ans=a[pos];
    }
    inline int read()
    {
        int x=0; char c=getchar();
        while(c<'0'||c>'9') c=getchar();
        while(c>='0'&&c<='9') { x=x*10+c-'0'; c=getchar();}
        return x;
    }
    void out(int x)
    {
        if(x/10) out(x/10);
        putchar(x%10+'0');
    }
    int main()
    {
        freopen("mex.in","r",stdin);
        freopen("mex.out","w",stdout);
        int q;
        n=read(); q=read();
        siz=sqrt(n)*1.5;
        for(int i=1;i<=n;i++) a[i]=read();
        for(int i=1;i<=q;i++)
        {
            e[i].l=read(); e[i].r=read();
            e[i].bl=(e[i].l-1)/siz+1;
            e[i].id=i;
        }
        sort(e+1,e+q+1,cmp);
        int opl,opr,l=1,r=0;
        for(int i=1;i<=q;i++)
        {
            opl=e[i].l; opr=e[i].r;
            while(opl<l) updata(--l,1,1);
            while(opr>r) updata(++r,1,1);
            while(opl>l) updata(l++,-1,2);
            while(opr<r) updata(r--,-1,2);
            an[e[i].id]=ans;
        }
        for(int i=1;i<=q;i++) 
         {
             out(an[i]);
             puts("");
         } 
    }
    View Code

    法二,主席树(用时是莫队的一半)

    以权值为下标,

    minn[i]=j,表示i所代表的权值为[l,r]的区间,最早出现的位置为j

    例:0 2 1 0 1 3 2

    对于root[7]来说,

    [0,3]=1, 0最早出现在第1个位置

    [1,3]=2, 2最早出现在第2个位置

    查询[l,r]时,在root[r]里查询

    如果左子区间的minn>=l,说明权值小的左边一半都最早出现在l以后,就往右孩子找

    否则,说明权值小的左边一半有没有出现在l之后的,就往左孩子找

    因为是在root[r]里,所以保证不超过右边界

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define N 200001
    using namespace std;
    int n,q,tot;
    int root[N],minn[N*20];
    
        int lc[N*20],rc[N*20];
        void change(int &now,int pre,int l,int r,int pos,int val)
        {
            if(!now) now=++tot;
            if(l==r) 
            {
                minn[now]=val;
                return;
            }
            int mid=l+r>>1;
            if(pos<=mid) 
            {
                change(lc[now],lc[pre],l,mid,pos,val);
                rc[now]=rc[pre];
            }
            else 
            {
                change(rc[now],rc[pre],mid+1,r,pos,val);
                lc[now]=lc[pre];
            }
            minn[now]=min(minn[lc[now]],minn[rc[now]]);
        }
        int query(int now,int l,int r,int pos)
        {
            if(l==r) return l;
            int mid=l+r>>1;
            if(minn[lc[now]]>=pos) return query(rc[now],mid+1,r,pos);
            return query(lc[now],l,mid,pos);
        }
    
    int main()
    {
        freopen("mex.in","r",stdin);
        freopen("mex.out","w",stdout);
        int m;
        scanf("%d%d",&n,&m);
        int x;
        for(int i=1;i<=n;i++) 
        {
            scanf("%d",&x);
            if(x>n) x=n;
            change(root[i],root[i-1],0,n,x,i);
        } 
        int l,r;
        while(m--)
        {
            scanf("%d%d",&l,&r);
            printf("%d
    ",query(root[r],0,n,l));
        }
    }
    View Code
  • 相关阅读:
    一周总结
    各个方法的不同和优缺点
    随机抽签程序报告
    一周总结
    一周总结
    一周总结
    数据库基本知识
    线程相关概念
    进程相关概念
    模拟ssh实现远程执行命令(socket)
  • 原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/6913992.html
Copyright © 2020-2023  润新知