Cutting Codeforces Round #493 (Div. 2)(贪心+思维)

Cutting

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]
→ two cuts → [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 x
and y numbers is |x−y| bitcoins. Find the maximum possible number of cuts that can be made while spending no more than B

bitcoins.

Input

First line of the input contains an integer n

(2≤n≤100) and an integer B (1≤B≤100

) — the number of elements in the sequence and the number of bitcoins you have.

Second line contains n
integers: a1, a2, …, an (1≤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 B

bitcoins.

Examples
Input

扫描二维码关注公众号,回复: 2913665 查看本文章
6 4
1 2 5 10 15 20

Output

1

Input

4 10
1 3 2 4

Output

0

Input

6 100
1 2 3 4 5 6

Output

2

Note

In the first sample the optimal answer is to split sequence between 2

and 5. Price of this cut is equal to 3

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 2
and 3, and between 4 and 5. The total price of the cuts is 1+1=2 bitcoins.

题意:

将一个数列,切成几个小的连续段,使得每段里面奇数和偶数个数相同,每切一次的花费为,切的位置的两侧的数字差的绝对值即|a[i]-a[i-1]|(如果在a[i]和a[i-1]之间切的话),给你一个数B,能使花费不超过B的最多切割次数

分析:

边输入边统计,当前奇数和偶数的个数,每当有奇数个数和偶数个数相同的时候,就记录下这个位置的花费,表示,这个位置可以切割,并且花费为cost元,最终得到所有可切割位置的花费之后,只需排个序,然后顺序加到大于B停止即可,输出加了几次

code:

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
int n,B;
int a[110],cost[110];
int o[110],e[110];
int main(){
    cin >> n >> B;
    int num = 0;
    for(int i = 0; i < n; i++){
        cin >> a[i];
        if(i != 0 && e[i-1] == o[i-1])
            cost[num++] = abs(a[i] - a[i-1]);
        if(a[i] & 1){
            if(i == 0) o[i] = 1;
            else{
                o[i] = o[i-1] + 1;
                e[i] = e[i-1];
            }
        }
        else{
            if(i == 0) e[i] = 1;
            else{
                e[i] = e[i-1] + 1;
                o[i] = o[i-1];
            }
        }
    }
    sort(cost,cost+num);
    int sum = 0;
    int ans = 0;
    for(int i = 0; i < num; i++){
        sum += cost[i];
        if(sum <= B) ans++;
        else break;
    }
    cout << ans << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/codeswarrior/article/details/81876184