Codeforces round 493 div2 B.Cutting (贪心技巧)

本文探讨了一个整数序列的切割问题,目标是在有限预算内最大化切割次数,确保每段序列的奇数与偶数数量相等。文章提供了一种贪心算法解决方案,并通过样例解释了实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Cutting
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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][4,1,2,3,4,5,4,4,5,5] two cuts [4,1|2,3,4,5|4,4,5,5][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 xx and yy numbers is |xy||x−y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than BB bitcoins.

Input

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

Second line contains nn integers: a1a1, a2a2, ..., anan (1ai1001≤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 BB bitcoins.

Examples
Input
Copy
6 4
1 2 5 10 15 20
Output
Copy
1
Input
Copy
4 10
1 3 2 4
Output
Copy
0
Input
Copy
6 100
1 2 3 4 5 6
Output
Copy
2
Note

In the first sample the optimal answer is to split sequence between 22 and 55. Price of this cut is equal to 33 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 22 and 33, and between 44 and 55. The total price of the cuts is 1+1=21+1=2 bitcoins.


#include<iostream> #include<cstring> #include<string> #include<vector> #include<cmath> #include<algorithm> using namespace std; #define maxn 1000 int num[10000]; int ans[maxn],cnt=0; /* 题目大意:给定n个数和一个上界, 一次切割的条件:切割后的每个子序列必须维护奇偶数的个数相同, 可见其有数学性质,这个性质不可能因为切割次数的增多而破坏。 那么,, 最小化的指标是什么?是切割次数; 有何限制?切割的代价和不超过上界。 分析原子状态,切割一次,代价加上左右数之差,切割次数加上一。 很容易让人想到背包问题,,,没错,我刚开始就是背包的思维走下去的, 后来发现误入歧途了。。。
一方面n实在是小的可怜,可以枚举出所有可切点的位置。 所以者显然就是个贪心问题了,对枚举出的所有断点, 排序然后遍历一遍贪心选取下即可。
*/ int main(){  int n,b;  cin>>n>>b;  int even=0;  int add=0;  for(int i=1;i<=n;++i)   cin>>num[i];  for(int i=1;i<n;++i)  {   if(num[i]&1)  even++;   else   add++;///   if(even!=add) continue;        ans[cnt++]=abs(num[i+1]-num[i]);  }///  sort(ans,ans+cnt);////  int sum=0,anss=0;///  for(int i=0;i<cnt;++i)  {////   sum+=ans[i];   if(sum<=b)   anss++;  }//////  cout<<anss<<endl;//  return 0; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值