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;
}

Cutting Codeforces Round #493 (Div. 2)的更多相关文章

  1. 【Codeforces Round #493 (Div. 2) B】Cutting

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然只有在前i个位置奇数偶数出现次数都相同的地方才能切. (且不管前面怎么切,这里都能切的. 那么就相当于有n个物品,每个物品的代价 ...

  2. Codeforces Round #493 (Div. 2) B. Cutting 前缀和优化_动归水题

    不解释,题目过水 Code: #include<cstdio> #include<cmath> #include<algorithm> using namespac ...

  3. Codeforces Round #493 (Div 2) (A~E)

    目录 Codeforces 998 A.Balloons B.Cutting C.Convert to Ones D.Roman Digits E.Sky Full of Stars(容斥 计数) C ...

  4. Codeforces Round #493 (Div. 2)

    C - Convert to Ones 给你一个01串 x是反转任意子串的代价 y是将子串全部取相反的代价 问全部变成1的最小代价 两种可能 一种把1全部放到一边 然后把剩下的0变成1  要么把所有的 ...

  5. Codeforces Round #493 (Div. 1)

    A. /* 发现每次反转或者消除都会减少一段0 当0只有一段时只能消除 这样判断一下就行 */ #include<cstdio> #include<algorithm> #in ...

  6. Codeforces Round #493 (Div. 2)D. Roman Digits 第一道打表找规律题目

    D. Roman Digits time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  7. Codeforces Round #493 (Div. 2) A. Balloons 贪心水题

    由于是输出任意一组解,可以将价值从小到大进行排序,第一个人只选第一个,第二个人选其余的.再比较一下第一个人选的元素和第二个人所选元素和是否相等即可.由于已将所有元素价值从小到大排过序,这样可以保证在有 ...

  8. Codeforces Round #493 (Div. 1) B. Roman Digits 打表找规律

    题意: 我们在研究罗马数字.罗马数字只有4个字符,I,V,X,L分别代表1,5,10,100.一个罗马数字的值为该数字包含的字符代表数字的和,而与字符的顺序无关.例如XXXV=35,IXI=12. 现 ...

  9. Codeforces Round #493 (Div. 2) C. Convert to Ones 乱搞_构造_好题

    题意: 给你一个长度为 nnn 的 010101串 ,你有两种操作: 1.将一个子串翻转,花费 XXX 2.将一个子串中的0变成1,1变成0,花费 YYY 求你将这个01串变成全是1的串的最少花费. ...

随机推荐

  1. 【原创】数据库基础之Mysql(2)主从库配置

    一 安装 # wget -i -c http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm# yum -y insta ...

  2. <?xml version="1.0" encoding="UTF-8"?> 的作用

    version="1.0" 声明用的xml版本是1.0 encoding="UTF-8" 声明用xml传输数据的时候的字符编码,假如文档里面有中文,编码方式不是 ...

  3. mysql 表结构及基本操作

    说明在mysql语句中,sql语句总共分四种 a.DDL数据定义语句=>常用的ddl语句有(CREATE[创建],DROP[删除],ALTER[修改表结构]) b.DML数据操作语句=>常 ...

  4. redis-hash

    Hash操作,redis中Hash在内存中的存储格式如下图: hset(name, key, value) # name对应的hash中设置一个键值对(不存在,则创建:否则,修改) # 参数: # n ...

  5. 如何连接LINUX服务器

    1.WINDOW下连接 使用PUTTY连接,链接如下:https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html 下载安装后打开,运行 ...

  6. 安卓开发app在后台运行时页面数据被系统清除后操作之重启APP

    在安卓开发过程中,当点击HOME键,将app运行在后台时,然后再点击app图标进入时,遇到了如下两种情况: 1.每次打开时,app的入口页面总是被执行. 2.当运行内存被其它应用占用完时,在进入app ...

  7. 一、Spring Boot 入门

    1.Spring Boot 简介 简化Spring应用开发的一个框架: 整个Spring技术栈的一个大整合: J2EE开发的一站式解决方案: 2.微服务 2014,martin fowler 微服务: ...

  8. CodeForces 959E Mahmoud and Ehab and the xor-MST (MST+找规律)

    <题目链接> 题目大意: 给定一个数n,代表有一个0~n-1的完全图,该图中所有边的边权为两端点的异或值,求这个图的MST的值. 解题分析: 数据较大,$10^{12}$个点的完全图,然后 ...

  9. struts2的java.lang.NoSuchMethodException错误

    不久前在学习struts时出现这个错误,在网上搜索了半天,发现答案不一.将其总结如下,以方便大家参考. 1. 你有没有试试看 其它的方法能不能用,要是都是这种情况的话,可能是你的Action类没有继承 ...

  10. Vim使用心得

    马上就要联赛啦. 学习一下vim 这是一个vim文档 http://vimcdoc.sourceforge.net/doc/ 这是一个优秀的vimrc配置 http://www.cnblogs.com ...