思维+双指针

Problem - 1690E - Codeforces

题意:

给我们n个礼物(n时偶数),每个礼物的重量为ai,现在让我们对这些礼物两两打包,打包后的重量等于两个礼物重量之和,并且你会得到(a1+a2)/k的贡献,让你最大化这个贡献。

思路:

首先每个礼物大于k的部分,一定会是有贡献的,这一部分我们可以提前计算。这样处理之后每个礼物剩下的都是%k之后的一个结果,我们对此排序,然后利用双指针计算还有多少个礼物给出贡献值,然后将两个结果加和,便是最后的答案。

代码:

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 1e2 + 10, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> pii;
int n, m, p;
void solve()
{
    ll n, k, ans = 0;
    cin >> n >> k;
    vector<ll> a(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
        ans += a[i] / k;
        a[i] %= k;
    }

    sort(a.begin(), a.end());
    int i = 0, j = n - 1;
    while (i < j)
    {
        if (a[i] + a[j] >= k)
            ans++, j--;
        i++;
    }
    cout << ans << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Problem - 1765D - Codeforces

题意:

给我们n段视频,我们每段视频都可以1min看完,而且每个视频有一个大小的参数ai M,由于我们的电脑太拉了,我们只能下载后再去观看视频,下载的速度恒定为1M/min,并且不能超过电脑的存储上限m,问我们看完这些所有视频最短需要多少时间。

思路:

如果我们不采取任何措施,那么我们看完这n段视频的总时间一定是 a1+a2+a3+......+an+ n,即我们看完一部下载一部,可是我们可以在观看视频的时候继续下载别的视频,这样我们就可以节约2min的时间,因此我们就可以利用排序后的视频时长进行双指针,如果这两个视频的不会超限,那么我们就可以节约2min的时间。注意最后一次的视频我们一定会画1min去观看。

代码:

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 1e2 + 10, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> pii;

void solve()
{
    ll n, m, sum = 0;
    cin >> n >> m;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
        cin >> a[i], sum += (a[i] + 1);

    sort(a.begin(), a.end());

    int i = 0, j = n - 1;
    while (i < j)
    {
        if (a[i] + a[j] <= m)
        {
            i++;
            if (i < j)
                sum -= 2;
            else
                sum--;
        }
        j--;
    }
    cout << sum << endl;
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    // cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

Problem - 1873G - Codeforces

题意:

给我们一个长度为n的字符串,”AB“可以替换成”BC“,”BA“可以替换成”CB“,每次替换我们可以得到1枚硬币,问我们这个字符串最多可以得到多少枚硬币。

思路:

我们稍微写一下就会发现AAAAAAB最后我们BCCCCCC和六个硬币,BAAAAAA会变成CBBBBBB

因此我们只需要枚举出当前的字符串有多少段连续的A和连续的B的个数,如果A的段数比B的个数多,那么我们的每一段A都可以变成C,并得到A的个数枚金币,否则的话,我们就会有形同与AAAA....BAAAA的字符串,那么我们只能舍弃长度最小的一段A,其余的都可以转化成C

代码如下:

#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
const int N = 1e2 + 10, mod = 1e9 + 7;
typedef long long ll;
typedef pair<int, int> pii;

void solve()
{
    string str;
    cin >> str;
    int n = str.size(), cnt = 0;
    vector<ll> a;
    for (int i = 0; i < n;)
    {
        int j = i;
        while (j < n && str[j] == 'A')
            j++;
        a.push_back(j - i);
        i = j;

        j = i;
        while (j < n && str[j] == 'B')
            j++;
        cnt += (j - i);
        i = j;
    }
    ll minn = 1e9, sum = 0;
    for (auto x : a)
        sum += x, minn = min(x, minn);
    if (!a.size() || !cnt)
    {
        cout << 0 << endl;
    }
    else
    {
        if (a.size() <= cnt)
            cout << sum << endl;
        else
            cout << sum - minn << endl;
    }
}

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值