cf Round #493 (Div. 2) B. Cutting

在这里插入图片描述
思路:先求符和条件的每个间隔的代价,想要获得最多次切割,贪心一下即可(按照代价从小到大依次切割)
注意代价是两边的差值取绝对值

#include <iostream>
#include<algorithm>
using namespace std;

int main()
{
    int n, b;
    scanf("%d%d", &n, &b);
    int arr[1010] = {0}, cost[1010] = {0};
    for(int i = 1; i <= n; i++)
    {
        scanf("%d", &arr[i]);
        cost[i] = 0;
    }
    int odd, even, cnt;
    odd = even = cnt = 0;
    for(int i = 1; i < n; i++)
    {
        if(arr[i] % 2 == 0)
            even++;
        else
            odd++;
        if(odd == even)
        {
            cost[cnt++] = abs(arr[i + 1] - arr[i]);
        }
    }
    sort(cost, cost + cnt);
    int tot = 0;
    for(int i = 0; i < cnt; i++)
    {
        if(b >= cost[i])
        {
            b -= cost[i];
            tot++;
        }
        else
        {
            printf("%d\n", tot);
            return 0;
        }
    }
    printf("%d\n", tot);
    return 0;
}

题目来源:https://codeforces.com/contest/998/problem/B

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值