编程语言
首页 > 编程语言> > 牛客算法周周练11水题

牛客算法周周练11水题

作者:互联网

Powered by:AB_IN 局外人

第一次练习赛AK。

A 切题之路

纯模拟,没什么好说的。

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
typedef unsigned long long ll;
const ll maxn=1e6;
using namespace std;
namespace IO{
    char ibuf[1<<21],*ip=ibuf,*ip_=ibuf;
    char obuf[1<<21],*op=obuf,*op_=obuf+(1<<21);
    inline char gc(){
        if(ip!=ip_)return *ip++;
        ip=ibuf;ip_=ip+fread(ibuf,1,1<<21,stdin);
        return ip==ip_?EOF:*ip++;
    }
    inline void pc(char c){
        if(op==op_)fwrite(obuf,1,1<<21,stdout),op=obuf;
        *op++=c;
    }
    inline ll read(){
        register ll x=0,ch=gc(),w=1;
        for(;ch<'0'||ch>'9';ch=gc())if(ch=='-')w=-1;
        for(;ch>='0'&&ch<='9';ch=gc())x=x*10+ch-48;
        return w*x;
    }
    template<class I>
    inline void write(I x){
        if(x<0)pc('-'),x=-x;
        if(x>9)write(x/10);pc(x%10+'0');
    }
    class flusher_{
    public:
        ~flusher_(){if(op!=obuf)fwrite(obuf,1,op-obuf,stdout);}
    }IO_flusher;
}
using namespace IO;
ll n,t,a,b,p[maxn],q[maxn],sum1,sum2,ans1,ans2;
int main()
{
    n=read();t=read();a=read();b=read();sum1=sum2=t;
    for(int i=1;i<=n;i++) p[i]=read();//时间
    for(int i=1;i<=n;i++) q[i]=read();//难度
    for(int i=1;i<=n;i++) {
        if(q[i]<a&&sum1>=p[i])sum1-=p[i],ans1++;
        if(q[i]<b&&sum2>=p[i])sum2-=p[i],ans2++;
        else if(q[i]>=b&&sum2>=2*p[i])sum2-=2*p[i],ans2++;
    }
    write(ans1);pc(32);write(ans2);
    return 0;
}

C 红球进黑洞

感觉大佬们好像是被题面吓到了。。其实就是单纯的模拟。
差点超时,不是最佳解。

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
typedef unsigned long long ll;
const ll maxn=1e6;
using namespace std;
namespace IO{
    char ibuf[1<<21],*ip=ibuf,*ip_=ibuf;
    char obuf[1<<21],*op=obuf,*op_=obuf+(1<<21);
    inline char gc(){
        if(ip!=ip_)return *ip++;
        ip=ibuf;ip_=ip+fread(ibuf,1,1<<21,stdin);
        return ip==ip_?EOF:*ip++;
    }
    inline void pc(char c){
        if(op==op_)fwrite(obuf,1,1<<21,stdout),op=obuf;
        *op++=c;
    }
    inline ll read(){
        register ll x=0,ch=gc(),w=1;
        for(;ch<'0'||ch>'9';ch=gc())if(ch=='-')w=-1;
        for(;ch>='0'&&ch<='9';ch=gc())x=x*10+ch-48;
        return w*x;
    }
    template<class I>
    inline void write(I x){
        if(x<0)pc('-'),x=-x;
        if(x>9)write(x/10);pc(x%10+'0');
    }
    class flusher_{
    public:
        ~flusher_(){if(op!=obuf)fwrite(obuf,1,op-obuf,stdout);}
    }IO_flusher;
}
using namespace IO;
ll n,t,a[maxn],op1,l,r,k,sum;
int main()
{
    n=read();t=read();
    for(ll i=1;i<=n;i++) a[i]=read();
    while(t--){
        op1=read();sum=0;
        if(op1==1){
            l=read();r=read();
            for(ll i=l;i<=r;i++) sum+=a[i];
            write(sum);pc('\n');
        }
        else{
            l=read();r=read();k=read();
            for(ll i=l;i<=r;i++) a[i]^=k;
        }
    }
}

D 游戏

正解是dfs+贪心。下面是菜鸡的偷鸡做法。
cin cout不能与IO的同用。

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
typedef unsigned long long ll;
const ll maxn=1e3+10;
using namespace std;
namespace IO{
    char ibuf[1<<21],*ip=ibuf,*ip_=ibuf;
    char obuf[1<<21],*op=obuf,*op_=obuf+(1<<21);
    inline char gc(){
        if(ip!=ip_)return *ip++;
        ip=ibuf;ip_=ip+fread(ibuf,1,1<<21,stdin);
        return ip==ip_?EOF:*ip++;
    }
    inline void pc(char c){
        if(op==op_)fwrite(obuf,1,1<<21,stdout),op=obuf;
        *op++=c;
    }
    inline ll read(){
        register ll x=0,ch=gc(),w=1;
        for(;ch<'0'||ch>'9';ch=gc())if(ch=='-')w=-1;
        for(;ch>='0'&&ch<='9';ch=gc())x=x*10+ch-48;
        return w*x;
    }
    template<class I>
    inline void write(I x){
        if(x<0)pc('-'),x=-x;
        if(x>9)write(x/10);pc(x%10+'0');
    }
    class flusher_{
    public:
        ~flusher_(){if(op!=obuf)fwrite(obuf,1,op-obuf,stdout);}
    }IO_flusher;
}
//using namespace IO;
char a[maxn][maxn];
int t,n,m;
int main(){
    scanf("%d",&t);
    while(t--){
        scanf("%d%d",&n,&m);
        //n=read();m=read();
        for(int i=1;i<=n;i++){
            cin>>a[i]+1;
        }
        if(a[1][1]=='R') puts("dreagonm");
        else if(a[1][1]=='G') puts("fengxunling");
        else puts("BLUESKY007");
    }
}

E 积木大赛

经典差分题,没啥好说的。。

#include <bits/stdc++.h>
#pragma GCC optimize(2)
#pragma GCC optimize(3)
typedef  long long ll;
const ll maxn=1e6;
using namespace std;
namespace IO{
    char ibuf[1<<21],*ip=ibuf,*ip_=ibuf;
    char obuf[1<<21],*op=obuf,*op_=obuf+(1<<21);
    inline char gc(){
        if(ip!=ip_)return *ip++;
        ip=ibuf;ip_=ip+fread(ibuf,1,1<<21,stdin);
        return ip==ip_?EOF:*ip++;
    }
    inline void pc(char c){
        if(op==op_)fwrite(obuf,1,1<<21,stdout),op=obuf;
        *op++=c;
    }
    inline ll read(){
        register ll x=0,ch=gc(),w=1;
        for(;ch<'0'||ch>'9';ch=gc())if(ch=='-')w=-1;
        for(;ch>='0'&&ch<='9';ch=gc())x=x*10+ch-48;
        return w*x;
    }
    template<class I>
    inline void write(I x){
        if(x<0)pc('-'),x=-x;
        if(x>9)write(x/10);pc(x%10+'0');
    }
    class flusher_{
    public:
        ~flusher_(){if(op!=obuf)fwrite(obuf,1,op-obuf,stdout);}
    }IO_flusher;
}
using namespace IO;
ll n,a[maxn],b[maxn],ans1,ans2;
int main()
{
    n=read();
    for(int i=1;i<=n;i++){
        a[i]=read();
        b[i]=a[i]-a[i-1];
        if(b[i]>0) ans1+=b[i];
        else ans2-=b[i];
    }
    write(max(ans1,ans2));
    pc('\n');
}

完结。

标签:ch,水题,flusher,ll,namespace,周周练,牛客,IO,obuf
来源: https://blog.csdn.net/qq_45859188/article/details/106794568