2021-12-15【Codeforces Round #760 (Div. 3)】【题解代码A-D】

A. Polycarp and Sums of Subsequences

题目描述

在这里插入图片描述

思路:只有7个数,暴力就完事了

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

const int maxn=1e6+5;
void solve(){
    int n = 7;
    int a[100]={0};
    for (int i = 0; i < n;i++){
        cin >> a[i];
    }
    for (int i = 0; i < 6;i++){
        for (int j = 0; j < 6;j++){
            for (int k = 0; k < 6;k++){
                if(i!=j&&j!=k&&i!=k&&(a[i]+a[j]+a[k]==a[6])){

                
                    cout << a[i] <<" "<< a[j] <<" " << a[k]<<endl;
                    return;
                }
            }
        }
    }
}

int main(){
    ios::sync_with_stdio(0);
    int t;
    cin >> t ;
    while(t--){
         solve();
    }
    return 0;
}

B. Missing Bigram

题目描述

在这里插入图片描述

思路:模拟题意,字母相同就删一个,长度不够用“a”补到n。

code:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

const int maxn=1e6+5;
void solve(){
    int n;
    cin >> n;
    string a, b;
    cin >> a;
    string cnt=a;
    for (int i = 1; i < n - 2;i++){
        cin>>b;
        if(a[1]==b[0])
            cnt += b.substr(1);
        else
            cnt += b;
        a = b;
    }
    while(cnt.size()<n)
        cnt += "a";
    cout << cnt << endl;
}

int main(){
    ios::sync_with_stdio(0);
    int t;
    cin >> t ;
    while(t--){
         solve();
    }
    return 0;
}

C. Paint the Array

题目描述

在这里插入图片描述

思路:求奇数位和偶数位所有数的最大公因数gcd(),用奇数gcd()检测偶数位的所有数,用偶数gcd()检测所有奇数位的数,如果不能被整除,则该gcd()为一个答案,否则为“0”。

code:


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;

const int maxn=1e6+5;
ll gcd(ll a, ll b){
    return a % b == 0 ? b : gcd(b, a % b);
}
int n;
bool check(int c,ll b,ll a[]){
    for (int i = c; i < n;i+=2){
        if(a[i]%b==0){
            return 0;
        }
    }
    return 1;
}
void solve()
{
    cin>>n;
    ll a1, b;
    ll a[105] = {0};
    for (int i = 0; i < n;i++){
        cin >> a[i];
    }
    if (n >= 4){
        a1 = gcd(a[0], a[2]);
        b = gcd(a[1], a[3]);
    }else if(n==3){
        a1 = gcd(a[0], a[2]);
        b = a[1];
    }else{
        a1 = a[0];
        b = a[1];
    }
    for (int i = 4;i<n;i+=2){
        a1 = gcd(a1, a[i]);
        if(i+1<n)
            b = gcd(b, a[i + 1]);
    }
    if(check(0,b,a)){
        cout << b << endl;
    }else if(check(1,a1,a)){
        cout << a1 << endl;
    }else
        cout << 0 << endl;
}

int main(){
    ios::sync_with_stdio(0);
    int t;
    cin >> t ;
    while(t--){
         solve();
    }
    return 0;
}

D. Array and Operations

题目描述

在这里插入图片描述

思路:一点点的 思维,数组排序后,对后2*k个树进行操作,由题意可知,题目中求最小的和,而分解到每一步高斯的结果要么为1,要么为0,所以用贪心的思想,使其尽可能为0。

code:


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int maxn=1e6+5;
int a[maxn],b[maxn];
void solve(){
    int n, k;
    cin >> n >> k;
    for (int i = 0;i<n;i++){
        cin >> a[i];
    }
    sort(a, a + n);
    ll sum = 0;
    for (int i = 0; i < n-(2*k);i++)
        sum += a[i];
    int m = 0;
    for (int i = n - (2 * k); i < n;i++){
        b[m++] = a[i];
    }
    for (int i = 0; i < k;i++){
        sum += b[i] / b[i+k];//关键
    }
        cout << sum << endl;
}

int main(){
    ios::sync_with_stdio(0);
    int t;
    cin >> t ;
    while(t--){
         solve();
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Eternity_GQM

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值