Cutting CodeForces - 998B

There are a lot of things which could be cut — trees, paper, “the rope”. In this problem you are going to cut a sequence of integers.

There is a sequence of integers, which contains the equal number of even and odd numbers. Given a limited budget, you need to make maximum possible number of cuts such that each resulting segment will have the same number of odd and even integers.

Cuts separate a sequence to continuous (contiguous) segments. You may think about each cut as a break between two adjacent elements in a sequence. So after cutting each element belongs to exactly one segment. Say, [4,1,2,3,4,5,4,4,5,5] → two cuts → [4,1|2,3,4,5|4,4,5,5]. On each segment the number of even elements should be equal to the number of odd elements.

The cost of the cut between x and y numbers is |x−y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B bitcoins.

Input
First line of the input contains an integer n (2≤n≤100) and an integer B (1≤B≤100) — the number of elements in the sequence and the number of bitcoins you have.

Second line contains n integers: a1, a2, …, an (1≤ai≤100) — elements of the sequence, which contains the equal number of even and odd numbers

Output
Print the maximum possible number of cuts which can be made while spending no more than B bitcoins.

Examples
Input
6 4
1 2 5 10 15 20
Output
1
Input
4 10
1 3 2 4
Output
0
Input
6 100
1 2 3 4 5 6
Output
2
Note
In the first sample the optimal answer is to split sequence between 2 and 5. Price of this cut is equal to 3 bitcoins.

In the second sample it is not possible to make even one cut even with unlimited number of bitcoins.

In the third sample the sequence should be cut between 2 and 3, and between 4 and 5. The total price of the cuts is 1+1=2 bitcoins.

题意:给出一个长度为n的序列,奇数个数和偶数个数相同,n为偶数个。
现在有一个操作,就是切分,需要保证切分出来的序列,奇数个数等于偶数个数。现在我们要切最多段,切一次需要花费d的比特币,d等于相邻2个数差值的绝对值,题目会给出一个总比特币数,问:不超过这个总比特币数的最多切割数是多少。

分析:首先,我们遍历这个数组,记录奇数和偶数的个数,当奇数和偶数的个数相同的时候,说明现在要切了,然后把花费记录起来,并且清空个数。继续跑。跑完后,我们把花费从小到大排列,因为要最大切割数,要从小的花费开始,这样才能保证切的数量最大,然后每次切一个,用总花费去剪,并且用一个count变量记录下切割次数即可。

一道水题,题目开始没这么读明白,看了大佬的解释恍然大悟。

AC代码:

#include <bits/stdc++.h>
using namespace std;
int const N=1000;
int n,m;
int a[N];
int sum[N];
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&a[i]);
	}
	int cnt=0;
	int odd=0;
	int even=0;
	int ans=0;
	for(int i=0;i<n;i++)
	{
		if(a[i]%2==1)
		{
			odd++;
		}
		else 
		{
			even++;
		}
		if(odd==even&&i!=n-1)
		{
			odd=0;
			even=0;
			sum[cnt++]=abs(a[i+1]-a[i]);
		}
	}
	sort(sum,sum+cnt);
	for(int i=0;i<cnt;i++)
	{
		if(m-sum[i]>=0)
		{
			m-=sum[i];
			ans++;
		}
	}
	printf("%d\n",ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值