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
#include
#include
#include
#include
using namespace std;
const int MAXN = 200;
bool ok(int a[], int left, int right) {
int x = 0, y = 0;
for(int i = left; i <= right; i++) {
if(a[i]%2==0) {
y++;
} else {
x++;
}
}
if(x == y) {
return true;
} else {
return false;
}
}
bool cmp(int a, int b) {
return a < b;
}
int main() {
int n, B;
while(scanf("%d %d", &n, &B) != EOF) {
int a[MAXN], t[MAXN];
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
int k = 0;
for(int i = 2; i < n; i += 2) {
if(ok(a, 1, i) && ok(a, i+1, n)) {
t[k++] = abs(a[i+1] - a[i]);
}
}
int ans = 0;
sort(t, t + k, cmp);
for(int i = 0; i < k; i++) {
if(B >= t[i]) {
ans++;
B -= t[i];
}
}
printf("%d\n", ans);
}
return 0;
}