Codeforces Educational Round #42

122 篇文章 0 订阅
52 篇文章 0 订阅

终于上紫了qaq
算是省选前的一个好消息了吧qaq
值得纪念。4.10-4.11

CF962A Equator(模拟)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 200010
inline char gc(){
    static char buf[1<<16],*S,*T;
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return  EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,a[N];ll sum=0,tmp=0;
int main(){
//  freopen("a.in","r",stdin);
    n=read();for(int i=1;i<=n;++i) a[i]=read(),sum+=a[i];
    for(int i=1;i<=n;++i){
        tmp+=a[i];if(tmp*2>=sum){printf("%d\n",i);return 0;}
    }
}

CF962B Students in Railway Carriage(贪心)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 200010
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,a,b,ans=0,last=0;
char s[N];
int main(){
//  freopen("a.in","r",stdin);
    n=read();a=read();b=read();scanf("%s",s+1);
    for(int i=1;i<=n;++i){
        if(s[i]=='*'){last=0;continue;}if(!a&&!b) break;
        if(last==1){if(b) --b,last=2,++ans;else last=0;continue;}
        if(last==2){if(a) --a,last=1,++ans;else last=0;continue;}
        if(a>b) --a,last=1,++ans;
        else --b,last=2,++ans;
    }printf("%d\n",ans);
    return 0;
}

CF962C Make a Square(爆搜)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 200010
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,ans=inf,bin[11],a[15],m=0;
char s[15];
void dfs(int x){
    if(x==n+1){
        if(!m) return;int res=0;
        for(int i=1;i<=m;++i) res=res*10+a[i];
        int xx=sqrt(res);if(xx*xx==res) ans=min(ans,n-m);return;
    }dfs(x+1);
    if(s[x]=='0'&&!m) return;
    a[++m]=s[x]-'0';dfs(x+1);--m;
}
int main(){
//  freopen("a.in","r",stdin);
    scanf("%s",s+1);n=strlen(s+1);
    dfs(1);if(ans==inf) puts("-1");
    else printf("%d\n",ans);
    return 0;
}

CF962D Merge Equals(stl)

其实就按输入顺序合并就好了qaq,开个map即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 150010
inline char gc(){
    static char buf[1<<16],*S,*T;
    if(S==T){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return  EOF;}
    return *S++;
}
inline int read(){
    int x=0,f=1;char ch=gc();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=gc();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=gc();
    return x*f;
}
int n,m;ll a[N];
map<ll,int>mp;
int main(){
//  freopen("a.in","r",stdin);
    n=read();m=n;
    for(int i=1;i<=n;++i){
        ll x=read();
        while(1){
            int y=mp[x];
            if(!y) break;--m;
            a[y]=0;mp[x]=0;x<<=1;
        }mp[x]=i;a[i]=x;
    }printf("%d\n",m);
    for(int i=1;i<=n;++i){
        if(a[i]) printf("%I64d ",a[i]);
    }return 0;
}

CF962E Byteland, Berland and Disputed Cities(贪心)

我们把同属两方的点称为红点。
我们考虑任意两个相邻的红点,他们之间的白点黑点怎么连。
对于所有白点,我们肯定是贪心地链接最近的一个白点/红点。
黑点同理。然后我们还可以把两个红点连起来,以节省一条白的和一条黑的,贪心取最优即可。
然后还要处理一下开头一段。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
#define N 200010
inline int read(){
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,last1=0,last2=0,last0=0;
ll ans=0,a[N];
int main(){
//  freopen("a.in","r",stdin);
    n=read();ll mx1=0,mx2=0;
    for(int i=1;i<=n;++i){
        a[i]=read();char op[5];scanf("%s",op+1);
        if(op[1]=='P'){
            if(!last0){
                if(last1) ans+=a[i]-a[last1];
                if(last2) ans+=a[i]-a[last2];
            }
            else{
                mx1=max(mx1,a[i]-a[last1]);ans+=a[i]-a[last1];
                mx2=max(mx2,a[i]-a[last2]);ans+=a[i]-a[last2];
                if(mx1+mx2>a[i]-a[last0]) ans+=a[i]-a[last0]-mx1-mx2;
            }last1=last2=last0=i;mx1=0;mx2=0;
        }if(op[1]=='B'){if(last1) mx1=max(mx1,a[i]-a[last1]),ans+=a[i]-a[last1];last1=i;}
        else{if(last2) mx2=max(mx2,a[i]-a[last2]),ans+=a[i]-a[last2];last2=i;}
    }printf("%I64d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值