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
过了pretest 却WA了final test 发现是手残sort函数下标写错了 已AC
思路就是通过统计每个位置之前有多少奇数和偶数得出能进行分割的有几个位置,算出这些位置分别需要花的代价,排个序贪心取即可。
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cstdio>
#include <iostream>
#include <assert.h>
#include <fstream>
using namespace std;
int main(){
int n,limit;
cin>>n>>limit;
int m[200];
int odd[200]={0},even[200]={0};
int bag[200];
int num=0;
for(int i=1;i<=n;i++){
cin>> m[i];
}
for(int i=1;i<=n;i++){
odd[i]=odd[i-1];
even[i]=even[i-1];
if(m[i]%2==1)
odd[i]++;
else
even[i]++;
if(odd[i]==even[i]){
bag[++num]=abs(m[i+1]-m[i]);
}
}
if(odd[n]==even[n])
num--;
if(odd[n]!=even[n]||n%2==1||num==0){
cout<<0<<endl;
return 0;
}
sort(bag+1,bag+num+1);
int sum=0;
for(int i=1;i<=num;i++){
sum+=bag[i];
if(sum>limit){
cout<<i-1<<endl;
break;
}
if(i==num){
cout<<num<<endl;
}
}
}