C/C++教程

Educational Codeforces Round 122 (Rated for Div. 2)

本文主要是介绍Educational Codeforces Round 122 (Rated for Div. 2),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

比赛链接

除夕夜就该打cf

难得a了四题,rk进一千了,新年上大分

A题给你个数,修改它最后一位让它变成7的倍数

#include<bits/stdc++.h>
#define N 10000
using namespace std;
template <typename T> inline void read(T& x) {
    x=0;T f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=x*f;
    return;
}
template <typename T,typename ...Arg>void read(T& x,Arg& ...arg){
    read(x);
    read(arg...);
}
template <typename T>inline void write(T x) {
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template <typename T,typename ...Arg>void write(T& x,Arg& ...arg){
    write(x);
    putchar(' ');
    write(arg...);
}
int a[N];
int main(){
    int T,n,x;
    read(T);
    while(T--){
        read(n);
        x=n%7;
        if(x==0) {printf("%d\n",n);continue;}
        if(n%10+7-x<10) n=n+7-x; else n-=x;
        printf("%d\n",n);
    }
}

B题给你个01串,对于它的所有子串Si,xi=当前子串0和1的个数中较少的那个(不能相等),求xi最大值

有思路就是极水题,选择整个串作为子串,如果01不等那直接出结果,如果相等就-1

#include<bits/stdc++.h>
#define N 10000
using namespace std;
template <typename T> inline void read(T& x) {
    x=0;T f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=x*f;
    return;
}
template <typename T,typename ...Arg>void read(T& x,Arg& ...arg){
    read(x);
    read(arg...);
}
template <typename T>inline void write(T x) {
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template <typename T,typename ...Arg>void write(T& x,Arg& ...arg){
    write(x);
    putchar(' ');
    write(arg...);
}
int a[N];
int main(){
    int T,n,x,zero,one,i,ans;
    string s;
    read(T);
    while(T--){
        cin>>s;
        zero=0;one=0;
        for(i=0;i<s.length();i++){
            if(s[i]=='0')zero++;else one++;
        }
        ans=min(zero,one);if(zero==one) ans--;
        printf("%d\n",ans);
    }
}

C 打怪物

你血量hc,攻击力dc,怪物血量hm,攻击力dm,你有k次强化机会,每次可以强化w攻击或a血量(直接加上去),问你能不能打死怪物

直接暴力,0次加攻循环到k次加攻即可

【一定要注意数据规模用long long】忘记用long long贡献了唯一一次错误提交,不然rk还能升好几百

#include<bits/stdc++.h>
#define N 10000
using namespace std;
template <typename T> inline void read(T& x) {
    x=0;T f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=x*f;
    return;
}
template <typename T,typename ...Arg>void read(T& x,Arg& ...arg){
    read(x);
    read(arg...);
}
template <typename T>inline void write(T x) {
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template <typename T,typename ...Arg>void write(T& x,Arg& ...arg){
    write(x);
    putchar(' ');
    write(arg...);
}
int a[N];
int main(){
    long long T,hc,dc,hm,dm,k,w,a,x;
    read(T);
    while(T--){
        read(hc,dc,hm,dm,k,w,a);
        bool f=false;
        for (x=0;x<=k;x++){
               if ((dc+x*w)*(((hc+(k-x)*a)+dm-1)/dm)>=hm)
            {printf("YES\n");f=true;break;}    
        }
        if(!f) printf("NO\n");
    }
}

D

01背包,n<=10^3,w<=10^6,存储要压缩一下空间变成一维的,几乎是裸题,预处理1到每个数字x的步数别处理错就行

#include<bits/stdc++.h>
#define N 10005
using namespace std;
template <typename T> inline void read(T& x) {
    x=0;T f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=x*f;
    return;
}
template <typename T,typename ...Arg>void read(T& x,Arg& ...arg){
    read(x);
    read(arg...);
}
template <typename T>inline void write(T x) {
    if(x<0)putchar('-'),x=-x;
    if(x<10)putchar(x+'0');
    else write(x/10),putchar(x%10+'0');
}
template <typename T,typename ...Arg>void write(T& x,Arg& ...arg){
    write(x);
    putchar(' ');
    write(arg...);
}
int a[N],b[N],c[N],w[N],ww[N],f[1000006];
int main(){
    int T,n,k,i,j;
    read(T);
    for(i=2;i<=1000;i++) w[i]=1000;
    w[1]=0;w[2]=1;
    for(i=2;i<=1000;i++){
        for(j=1;j<=i;j++){
            int tmp=i/j;
            w[i+tmp]=min(w[i+tmp],w[i]+1);
        }
    }
    while(T--){
        read(n,k);
        for(i=1;i<=n;++i) {read(b[i]);ww[i]=w[b[i]];}
        for(i=1;i<=n;++i) read(c[i]);
        for(i=0;i<=k;++i) f[i]=0;  
        for(i=1;i<=n;++i){
            for(j=k;j>=ww[i];j--){
//                printf("!!%d %d\n",j-ww[i],c[i]);
                f[j]=max(f[j],f[j-ww[i]]+c[i]);
            }
        }
//        for(i=1;i<=k;i++) printf("%d-",f[i]);
        printf("%d\n",f[k]);
    }
}

 

这篇关于Educational Codeforces Round 122 (Rated for Div. 2)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!