Codeforces Round 961 (Div. 2) A - C

视频讲解~~

Codeforces Round 961 (Div. 2) A - C_哔哩哔哩_bilibili

看视频噢


A - Diagonals

#include<bits/stdc++.h>
using namespace std;

int solve(int n, int k)
{
    if (k == 0)
        return 0;
    if (n >= k)
        return 1;
    k -= n;
    int cnt = 1;
    for (int i = n - 1; i >= 1; i--)
    {
        if (i >= k)
            return cnt + 1;
        cnt++, k -= i;
        if (i >= k)
            return cnt + 1;
        cnt++, k -= i;
    }
    return -1;
}

int main()      
{
    int T;
    cin >> T;
    while (T--)
    {
        int n, k;
        cin >> n >> k;
        cout << solve(n, k) << endl;
    }
}

B1 - Bouquet (Easy Version)

#include<bits/stdc++.h>
using namespace std;
#define int long long

signed main()      
{
    int T;
    cin >> T;
    while (T--)
    {
        int n, m;
        cin >> n >> m;
        vector<int> a(n);
        for (auto &i : a)
            cin >> i;
        sort(a.begin(), a.end());
        int sum = 0, ans = 0;
        for (int l = 0, r = 0; r < n; r++)
        {
            sum += a[r];
            while (l < r && !(sum <= m && a[r] - a[l] <= 1))
                sum -= a[l], l++;
            if (sum <= m && a[r] - a[l] <= 1)
                ans = max(ans, sum);
        }
        cout << ans << endl;
    }
}

B2 - Bouquet (Hard Version)

#include<bits/stdc++.h>
using namespace std;
#define int long long

signed main()      
{
    int T;
    cin >> T;
    while (T--)
    {
        int n, m;
        cin >> n >> m;
        vector<pair<int, int>> a(n);
        for (auto &i : a)
            cin >> i.first;
        for (auto &i : a)
            cin >> i.second;
        sort(a.begin(), a.end());
        int ans = 0;
        for (auto [i, j] : a)
            ans = max(ans, i * min(j, (m / i)));

        for (int i = 1; i < n; i++)
        {
            if (a[i - 1].first + 1 < a[i].first)
                continue;
            int cur = m;
            int small = min(a[i - 1].second, cur / a[i - 1].first);
            cur -= small * a[i - 1].first;
            int big = min(a[i].second, cur / a[i].first);
            cur -= big * a[i].first;
            int temp = min(small, a[i].second - big);
            int add = min(cur, temp);
            cur -= add;
            ans = max(ans, m - cur);
        }
        cout << ans << endl;
    }
}

C - Squaring

#include<bits/stdc++.h>
using namespace std;
#define int long long

bool ok(vector<int> &a)
{
    int n = a.size();
    for (int i = 1; i < n; i++)
        if (a[i] == 1 && a[i - 1] > 1)
            return false;
    return true;
}

int cal(int a, int b, int c)
{
    if (a == 1 || c == 1)
        return 0;
    double it = log2(a) / log2(c);
    int x = ceil(b + log2(it));
    return max(0LL, x);
}

signed main()      
{
    int T;
    cin >> T;
    while (T--)
    {
        int n;
        cin >> n;
        vector<int> a(n), b(n, 0);
        for (auto &i : a)
            cin >> i;
        if (!ok(a))
        {
            cout << -1 << endl;
            continue;
        }
        int cnt = 0;
        for (int i = 1; i < n; i++)
            b[i] = cal(a[i - 1], b[i - 1], a[i]), cnt += b[i];
        cout << cnt << endl;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值