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 |x−y||x−y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than BB bitcoins.
First line of the input contains an integer nn (2≤n≤1002≤n≤100) and an integer BB (1≤B≤1001≤B≤100) — the number of elements in the sequence and the number of bitcoins you have.
Second line contains nn integers: a1a1, a2a2, ..., anan (1≤ai≤1001≤ai≤100) — elements of the sequence, which contains the equal number of even and odd numbers
Print the maximum possible number of cuts which can be made while spending no more than BB bitcoins.
6 4 1 2 5 10 15 20
1
4 10 1 3 2 4
0
6 100 1 2 3 4 5 6
2
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
题意:将一组奇偶数数量相同的数cut一下,使每部分的奇偶数数量相同,每次cut都需要花费| a[i]-a[i-1] |的bitcoin,求花费不超过B的bition时,最大的cut数。
题解:遍历一遍,记录所有可以cut的地方要花费的bition,我是记录在b【】数组中,注意取绝对值,然后用贪心的思想,排序,从小到大取。
代码:如果觉得题解可以欢迎关注评论,如果觉得不好,也欢迎评论提意见,我会加以改进的
#include <bits/stdc++.h>
using namespace std;
int n;
int a[110],b[110];
int o[110],e[110];
int bt;
int main()
{
cin>>n>>bt;
int num=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
if(i!=0&&e[i-1]==o[i-1])
{
//cout<<a[i]<<" "<<a[i-1]<<endl;
b[num++]=abs(a[i]-a[i-1]);
}
if(a[i]%2==0)
{
if(i==0)
e[i]=1;
else
{
e[i]=e[i-1]+1;
o[i]=o[i-1];
}
}
else
{
if(i==0)
o[i]=1;
else
{
o[i]=o[i-1]+1;
e[i]=e[i-1];
}
}
}
sort(b,b+num);
int sum=0;
int t=0;
for(int i=0;i<num;i++)
{
sum+=b[i];
if(sum<=bt)
t++;
else
break;
}
cout<<t<<endl;
}