Codeforces Round #767 (Div. 2)——A~B

本文介绍了两道算法题目,分别是通过获取能量最大化的问题和通过操作数组实现最大公约数大于1的情况。在A题中,通过排序和累加策略找到最大能量;在B题中,讨论了如何通过最少操作次数使数组的最大公约数大于1,重点在于判断数组中奇数的数量。
摘要由CSDN通过智能技术生成

A. Download More RAM

题目链接

题意: 给定数组a、b,当前你有的k的能量,如果你能够达到a[i],那么当前你的能量+b[i],求你能达到的最大的能量。

题解: 我们给a、b数组排序,尽可能的加,得到最终答案

代码:

#include <iostream>
#include <vector>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>

#define endl '\n'
#define _for(i, a, b)  for (int i = (a); i < (b); i ++ )
#define _rep(i, a, b)  for (int i = (a); i <= (b); i ++ )

using namespace std;

const int N = 110;
struct node
{
    int a, b;
}t[N];

bool cmp (node t1, node t2)
{
    return t1.a < t2.a;
}

int T, n, k;

int main ()
{

    ios :: sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    cin >> T;
    while (T -- )
    {
        cin >> n >> k;
        _for(i, 0, n)    cin >> t[i].a;
        _for(i, 0, n)    cin >> t[i].b;
        sort(t, t + n, cmp);
        _for(i, 0, n)    if (k >= t[i].a)   k += t[i].b;
        cout << k << endl;
    }

    return 0;
}

B. GCD Arrays

题目链接

题意: 给定数组a,其中数据为[l, r],定义gcd(a)为a数组中的最大公约数,你可以进行以下操作:选择数组中的两个数,将他们从数组a是移除,然后加入它们的乘积。
问:能否在操作次数不超过k的情况下,使得gcd(a) > 1?输出YES 或者 NO。

题解: 首先我们要知道如果要使得gcd(a) > 1,那么a数组中的所有元素必须有一个共同的因子,又因为a中的数组元素是从l到r连续的,所以明显2是最容易出现的因子。那么现在问题就转化为:将数组中的奇数全都转化成偶数最少次数是多少?
从已知条件我们可以清楚地知道,一个偶数和一个奇数搭配后操作一次即可使得奇数消失,所以最少的次数就是数组中奇数的个数,易知奇数的个数:r - l + 1 - (r / 2 - (l - 1) / 2);前面的是数组元素的个数,后面的是数组中偶数的个数。
然后将奇数的个数 t 和 k 比较,如果 t <= k,输出YES,否则NO。
同时我们还要考虑一下特殊情况,当l == r 当且仅当 l = 1时为NO,否则为YES。

代码:

#include <iostream>
#include <vector>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <iomanip>

#define endl '\n'
#define _for(i, a, b)  for (int i = (a); i < (b); i ++ )
#define _rep(i, a, b)  for (int i = (a); i <= (b); i ++ )

using namespace std;

int main ()
{

    ios :: sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int T;    cin >> T;

    while (T -- )
    {
        int l, r, k;    cin >> l >> r >> k;
        int t = r - l + 1 - (r / 2 - (l - 1) / 2);
        if (l == r)
        {
            if(l == 1)    cout << "NO" << endl;
            else    cout << "YES" << endl;
        }
        else
        {
            if (t <= k)    cout << "YES" << endl;
            else    cout << "NO" << endl;
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值