998B Cutting

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个数ai,奇数个数和偶数个数相同,然后你可以在奇数和偶数个数相等的时候,你就可以剪一个片段,需要花费|a[i]-a[i+1]|。现在总共预算是b,让你在b预算内,尽可能的多剪片段,输出最多的剪的次数。

题解:贪心  先把所有能减的片段,奇数和偶数个数相等的时候,把花费存起来,然后排个序,从前面累加,如果没有超出预算b剪的次数就++

c++:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,m,a[110],ans=0,cnt1=0,cnt2=0,b[110],c=0,sum=0;
    cin>>n>>m;
    for(int i=0;i<n;i++)
         cin>>a[i];
    for(int i=0;i<n;i++)
    {
        if(a[i]%2) cnt1++;
         else cnt2++;
         if(cnt1==cnt2&&i+1<n)
           b[c++]=abs(a[i]-a[i+1]);
    }
    sort(b,b+c);
    for(int i=0;i<c;i++)
    {
        sum+=b[i];
        if(sum<=m)
            ans++;
        else break;
    }
    cout<<ans;
    return 0;
}

python:

n,m=map(int,input().split())
a=list(map(int,input().split()))
b=[]
cnt1=0;cnt2=0
for i in range(len(a)):
    if a[i]%2==0:cnt1+=1
    else:cnt2+=1
    if cnt1==cnt2 and i+1<len(a):
        b.append(abs(a[i]-a[i+1]))
b.sort()
sum=0;ans=0
for i in b:
    sum+=i
    if sum<=m:
        ans+=1
    else:
       break
print(ans)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落凡尘.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值