牛客算法週週練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');
}

完結。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章