Cutting(CF-998B)

博客围绕整数序列分割问题展开,给定含相同奇偶个数的整数序列,在 x 和 y 元素间分割花费 |x - y| 元,有 k 元预算。需找出最多分割次数,思路是统计奇偶个数,将符合条件的花费放入数组,用贪心算法选最小花费。

Problem Description

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

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

题意:给出 n 个数字,保证奇数个数和偶数个数相等,可以把该序列分割成连续的片段,使得每一段的奇数和偶数个数相等,假设在 x 和 y 元素中间分割,那么花费 |x-y| 块钱,现有k块钱,求最多可以分割的次数

思路:从开头统计奇数和偶数的个数,如果某个位置奇数和偶数个数相等就把当前这个花费放入数组,然后贪心的选择最小的即可

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<ctime>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 100001
#define MOD 1e9+7
#define E 1e-6
#define LL long long
using namespace std;
int a[105];
int b[105],c[105],x[105];
int main()
{
   int n,m;
   cin>>n>>m;
   for(int i=1;i<=n;i++)
   {
       scanf("%d",&x[i]);

       if(x[i]%2==0)
           a[i]=a[i-1]+1;
       else
           a[i]=a[i-1];
       
       if(x[i]%2!=0)
           b[i]=b[i-1]+1;
       else
           b[i]=b[i-1];
   }

   int k=0;
   for(int i=1;i<=n;i++)
   {
       if(a[i]==b[i]&&i!=n)
        {
            k++;
            c[k]=fabs(x[i+1]-x[i]);
        }

   }
  
  sort(c+1,c+k+1);
  
  int ans=0;
  for(int i=1;i<=k;i++)
  {
      if(m-c[i]<0) 
          break;
      m-=c[i];
      ans++;
  }
  cout<<ans<<endl;
  return 0;
}

 

我在安装onnxsim时,出现错误:(ssd-gpu) C:\Users\z>pip install onnxsim Looking in indexes: https://mirrors.aliyun.com/pypi/simple/ Collecting onnxsim Using cached https://mirrors.aliyun.com/pypi/packages/ce/9e/f34238413ebeda9a3a8802feeaa5013934455466b9ab390b48ad9c7e184f/onnxsim-0.4.36.tar.gz (21.0 MB) Preparing metadata (setup.py) ... done Requirement already satisfied: onnx in d:\program_file\anaconda3\envs\ssd-gpu\lib\site-packages (from onnxsim) (1.17.0) Collecting rich (from onnxsim) Downloading https://mirrors.aliyun.com/pypi/packages/19/71/39c7c0d87f8d4e6c020a393182060eaefeeae6c01dab6a84ec346f2567df/rich-13.9.4-py3-none-any.whl (242 kB) Requirement already satisfied: numpy>=1.20 in d:\program_file\anaconda3\envs\ssd-gpu\lib\site-packages (from onnx->onnxsim) (1.26.4) Requirement already satisfied: protobuf>=3.20.2 in d:\program_file\anaconda3\envs\ssd-gpu\lib\site-packages (from onnx->onnxsim) (4.25.3) Collecting markdown-it-py>=2.2.0 (from rich->onnxsim) Downloading https://mirrors.aliyun.com/pypi/packages/42/d7/1ec15b46af6af88f19b8e5ffea08fa375d433c998b8a7639e76935c14f1f/markdown_it_py-3.0.0-py3-none-any.whl (87 kB) Collecting pygments<3.0.0,>=2.13.0 (from rich->onnxsim) Downloading https://mirrors.aliyun.com/pypi/packages/8a/0b/9fcc47d19c48b59121088dd6da2488a49d5f72dacf8262e2790a1d2c7d15/pygments-2.19.1-py3-none-any.whl (1.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 6.2 MB/s eta 0:00:00 Collecting mdurl~=0.1 (from markdown-it-py>=2.2.0->rich->onnxsim) Downloading https://mirrors.aliyun.com/pypi/packages/b3/38/89ba8ad64ae25be8de66a6d463314cf1eb366222074cfda9ee839c56a4b4/mdurl-0.1.2-py3-none-any.whl (10.0 kB) Building wheels for collected packages: onnxsim Building wheel for onnxsim (setup.py) ... error error: subprocess-exited-with-error × python setup.py bdist_wheel did not run successfully. │ exit code: 1 ╰─> [154 lines of output] C:\Users\z\AppData\Local\Temp\pip-install-8tsnq_p0\onnxsim_88d298981e5d40c492e3f0d92d52ac0e
最新发布
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值