北華大學第五屆程序設計競賽春季聯賽水題

Powered by:AB_IN 局外人

A 遺忘的泰勒

t=int(input())
while t>0:
    t-=1
    n=int(input())
    if n&1:
        print("YES")
    else:
        print("NO")

B 多餘的字母

t=int(input())
while t>0:
    t-=1
    s=input().strip()
    for i in s:
        if i.islower():
            print(i,end="")
    print()

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 int read(){
        register int 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;
}
ll x,y,t,nx,ny;
int main()
{
    using namespace IO;
    t=read();
    while(t--){
        ll ans=0;
        x=read();y=read();
        if(x==y){
            write(x*4);pc('\n');
            continue;
        }
        else{
            while(1){
            nx=min(x,y);
            ny=max(x,y);
            if(nx==1)
            {ans+=ny*4;break;}
            ny-=nx;
            ans+=nx*4;
            x=nx;
            y=ny;
            if(nx==ny)
            {ans+=nx*4;break;}
            }
        }
        write(ans);pc('\n');
    }
}

D 最大的收益

DP

  1. dp[i][1]代表這次我選這個數,那麼總數就是這個數和上個不選這個數的值相加。
  2. dp[i][0]代表這次我不選這個數,那麼這個值就是上一個狀態兩個值中的最大值。
#include <bits/stdc++.h>
typedef long long ll;
const ll maxn=1e4;
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 int read(){
        register int 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;
}
ll a[maxn],dp[maxn][2],t,n;
int main()
{
    using namespace IO;
    t=read();
    while(t--)
    {
        n=read();
        for(int i=1;i<=n;i++)
            a[i]=read();
        dp[1][0]=0;
        dp[1][1]=a[1];
        for(int i=2;i<=n;i++)
        {
            dp[i][1]=dp[i-1][0]+a[i];
            dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
        }
        write(max(dp[n][0],dp[n][1]));
        pc('\n');
    }
}

完結。

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