Codeforces Global Round 8

Codeforces Global Round 8


B. Codeforces Subsequences

题意:

给定n,要求构造一个字符串,满足该字符串有至少n个子序列为“codeforces”
串的长度尽可能小

数据范围:n<=1e16

解法:

n=1的时候,codeforces
n=2的时候,ccodeforces(2x1x1x1x1x1x1x1x1x1)
n=3的时候,ccoodeforces(2x2x1x1x1x1x1x1x1x1)

尽量让每个位置的字符重复量相同,这样才能在尽量短的条件下使得子序列“codeforces”的数量最多
贪心的从左到右依次增加字符数即可

code:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxm=2e5+5;
int a[maxm];
const string s="codeforces";
const int len=s.size();
signed main(){
    int n;cin>>n;
    for(int i=0;i<len;i++){
        a[i]=1;
    }
    int sum=1;
    int k=0;
    while(sum<n){
        sum/=a[k];
        a[k]++;
        sum*=a[k];
        k=(k+1)%10;
    }
    for(int i=0;i<len;i++){
        for(int j=0;j<a[i];j++){
            cout<<s[i];
        }
    }
    return 0;
}

C. Even Picture

题意:

给定n,要求在格子图中构造出k个点的坐标,满足:

  • k个点连通
  • 与每个点相邻的点的个数为偶数个
  • 4个方向都有相邻点的点的个数为n个

例如n=4时:
在这里插入图片描述

数据范围:n<=500

解法:

在这里插入图片描述

code:
#include<bits/stdc++.h>
using namespace std;
signed main(){
    int n;cin>>n;
    cout<<3*n+4<<endl;
    int nowx=1;
    int nowy=1;
    cout<<nowx<<' '<<nowy<<endl;
    cout<<nowx<<' '<<nowy+1<<endl;
    nowx++;
    for(int i=1;i<=n;i++){
        cout<<nowx<<' '<<nowy<<endl;
        cout<<nowx<<' '<<nowy+1<<endl;
        cout<<nowx<<' '<<nowy+2<<endl;
        nowx++,nowy++;
    }
    cout<<nowx<<' '<<nowy<<endl;
    cout<<nowx<<' '<<nowy+1<<endl;
    return 0;
}

D. AND, OR and square sum

题意:

给定长度为n的数组a,
一次操作中你可以选择两个下标i和j,然后令a[i]=a[i]&a[j],a[j]=a[i]|a[j](一个进行位运算与&一个进行位运算或|)
你可以进行无限次操作,问操作之后sum(a[i]2)的最大值是多少?

数据范围:n<=2e5

解法:

当a[i]和a[j]的第x个二进制位上相同的时候,a[i]&a[j]和a[i]|a[j]的结果相同。
当a[i]和a[j]的第x个二进制位上不同德时候,a[i]&a[j]=0,a[i]|a[j]=1,相当于可以将一个数这一位上的1移动到另外一个数上。

综上得:两个数进行操作之后二进制位上的1不会增加,只会发生移动。

因此可以贪心的将所有的二进制1优先集中到一个数上,这样能使得最后的答案最大。

统计所有数每一个二进制位上1的个数,贪心分配即可。

code:
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int maxm=2e5+5;
int a[maxm];
int cnt[30];
signed main(){
    int n;cin>>n;
    for(int i=1;i<=n;i++){
        int x;cin>>x;
        for(int j=0;j<30;j++){
            cnt[j]+=(x>>j&1);
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        int x=0;
        for(int j=0;j<30;j++){
            if(cnt[j]){
                cnt[j]--;
                x+=(1<<j);
            }
        }
        ans+=x*x;
    }
    cout<<ans<<endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值