Cutting CodeForces - 998B (思维)

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: a1a1a2a2, ..., 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

题意:给出长为n的数列,n为偶数,并且有n/2个奇数,n/2个偶数,现在,你要尽可能多的将这个数列划分成奇数和偶数个数相同的区间。每一次划分的花费就是你划分之间的两个数差的绝对值。你的花费不能超过m.问,你最多可以划分几次。


思路:直接循环,然后记录奇数和偶数的个数l,r。如果l==r,那么记录这个划分的花费ans[i],然后重置l和r。最后,将ans按升序排列,从花费小的开始划分,如果m大于0那么可以划分,m-=ans[i],否则,退出循环。


#include "iostream"
#include "cmath"
#include "algorithm"
using namespace std;
int a[105],ans[100];
int main()
{
    ios::sync_with_stdio(false);
    int n,m,t=0,sum=0,l,r;
    l=r=0;
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n-1;i+=2){
        a[i]%2?l++:r++;
        a[i+1]%2?l++:r++;
        if(l==r&&i!=n-1){ans[t++]=abs(a[i+2]-a[i+1]);l=r=0;}
    }
    sort(ans,ans+t);
    for(int i=0;i<t;i++){if(m-ans[i]>=0){m-=ans[i],sum++;}}
    cout<<sum<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41874469/article/details/80881892