Cutting Codeforces Round #493 (Div. 2)

Cutting

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 Bbitcoins.
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
123456789101112131415161718192021222324
一道很简单的暴力题,但是一直没有抓住关键。
问题的性质:每个切点之间没有关系,即某个切点切还是不切不影响其他的切点,需要看出来他们之间的不关联性。
附ac码

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int a[105];		//记录原数组
int b[105];		//记录奇数个数的前缀和
int c[105];		//记录所有的切点
int n,s,tmp;
int cnt=0,ans;

int main()
{
	while(~scanf("%d%d",&n,&s))
	{
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
		memset(c,0,sizeof(c));
		ans=0;
		cnt=0;
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			b[i]=a[i]%2+b[i-1];
		}
		if(n%2==0 && b[n]==n/2)		//如果奇数个数字或者整个数列中奇数和偶数的个数不相等显然切不成
		{
			for(int i=2;i<n;i+=2)
			{
				if(b[i]==i/2)
				c[cnt++]=abs(a[i+1]-a[i]);
			}
			sort(c,c+cnt);
			tmp=0;
			for(int i=0;i<cnt;i++)
			{
				if(tmp+c[i]<=s)
				{
					tmp+=c[i];
					ans++;
				}
				else
				{
					break;
				}
			}
		}
		printf("%d\n",ans);
	}
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值