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 Bbitcoins.
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
123456789101112131415161718192021222324
一道很简单的暴力题,但是一直没有抓住关键。
问题的性质:每个切点之间没有关系,即某个切点切还是不切不影响其他的切点,需要看出来他们之间的不关联性。
附ac码
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[105]; //记录原数组
int b[105]; //记录奇数个数的前缀和
int c[105]; //记录所有的切点
int n,s,tmp;
int cnt=0,ans;
int main()
{
while(~scanf("%d%d",&n,&s))
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
ans=0;
cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i]%2+b[i-1];
}
if(n%2==0 && b[n]==n/2) //如果奇数个数字或者整个数列中奇数和偶数的个数不相等显然切不成
{
for(int i=2;i<n;i+=2)
{
if(b[i]==i/2)
c[cnt++]=abs(a[i+1]-a[i]);
}
sort(c,c+cnt);
tmp=0;
for(int i=0;i<cnt;i++)
{
if(tmp+c[i]<=s)
{
tmp+=c[i];
ans++;
}
else
{
break;
}
}
}
printf("%d\n",ans);
}
return 0;
}
Cutting Codeforces Round #493 (Div. 2)的更多相关文章
- 【Codeforces Round #493 (Div. 2) B】Cutting
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然只有在前i个位置奇数偶数出现次数都相同的地方才能切. (且不管前面怎么切,这里都能切的. 那么就相当于有n个物品,每个物品的代价 ...
- Codeforces Round #493 (Div. 2) B. Cutting 前缀和优化_动归水题
不解释,题目过水 Code: #include<cstdio> #include<cmath> #include<algorithm> using namespac ...
- Codeforces Round #493 (Div 2) (A~E)
目录 Codeforces 998 A.Balloons B.Cutting C.Convert to Ones D.Roman Digits E.Sky Full of Stars(容斥 计数) C ...
- Codeforces Round #493 (Div. 2)
C - Convert to Ones 给你一个01串 x是反转任意子串的代价 y是将子串全部取相反的代价 问全部变成1的最小代价 两种可能 一种把1全部放到一边 然后把剩下的0变成1 要么把所有的 ...
- Codeforces Round #493 (Div. 1)
A. /* 发现每次反转或者消除都会减少一段0 当0只有一段时只能消除 这样判断一下就行 */ #include<cstdio> #include<algorithm> #in ...
- Codeforces Round #493 (Div. 2)D. Roman Digits 第一道打表找规律题目
D. Roman Digits time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- Codeforces Round #493 (Div. 2) A. Balloons 贪心水题
由于是输出任意一组解,可以将价值从小到大进行排序,第一个人只选第一个,第二个人选其余的.再比较一下第一个人选的元素和第二个人所选元素和是否相等即可.由于已将所有元素价值从小到大排过序,这样可以保证在有 ...
- Codeforces Round #493 (Div. 1) B. Roman Digits 打表找规律
题意: 我们在研究罗马数字.罗马数字只有4个字符,I,V,X,L分别代表1,5,10,100.一个罗马数字的值为该数字包含的字符代表数字的和,而与字符的顺序无关.例如XXXV=35,IXI=12. 现 ...
- Codeforces Round #493 (Div. 2) C. Convert to Ones 乱搞_构造_好题
题意: 给你一个长度为 nnn 的 010101串 ,你有两种操作: 1.将一个子串翻转,花费 XXX 2.将一个子串中的0变成1,1变成0,花费 YYY 求你将这个01串变成全是1的串的最少花费. ...
随机推荐
- 【原创】Linux基础之挂载硬盘
1 查看哪些硬盘没有挂载 # fdisk -l Disk /dev/vdb: 107.4 GB, 107374182400 bytes, 209715200 sectorsUnits = sector ...
- 网络流24题 ——运输问题 luogu 4015
题目描述:这里 题面已经提示我们这是费用流了 那么由源点向所有仓库连边,容量为仓库原有货物量,费用为0 然后由所有零售商店向汇点连边,容量为一个零售商店的需求量,费用为0 最后由仓库向零售商店连边,容 ...
- Git使用六:版本对比
准备工作: 创建一个新的项目,并初始化git 创建两个文件,并写入对应内容(utf-8无bom格式) 执行git add 命令将两个文件添加到暂存区,执行commit命令提交到仓库并生产快照 修改工作 ...
- 20165328 实验四《Andriid应用开发》实验报告
一.实验一:Andriod Stuidio的安装测试: 实验要求: 参考 ...
- vue+element ui 表格自定义样式溢出隐藏
样式 .hoveTitle { text-align: left; width: 140px; overflow: hidden; text-overflow: ellipsis; white-spa ...
- 遍历文件路径python版,java版
python: # 获取所有txt路径列表 file_list = [] def gci(filepath): files=os.listdir(filepath) for fi in files: ...
- TreeMap 的排序冲突吗
今天在网上看到一个问题:一个已经构建好的 TreeSet,怎么完成倒排序? 网上给出的答案是: 通过TreeSet构造函数传入一个比较器,指定比较器进行排序为原排序的倒叙. TreeSet的自然排序是 ...
- Auth模块、Forms组件
Auth模块 auth模块是Django自带的用户认证模块: 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这 ...
- 我的 FPGA 学习历程(02)—— 实验:点亮 LED 灯
关于 Quartus 的操作可以使用 Quartus 自带的帮助,帮助中带有全套的操作教程. 中文网络教程链接(链接至 altera中文官网,点击观看) Quartus II 软件设计系列:基础 Qu ...
- Web 录音
所需文件下载地址 链接:https://pan.baidu.com/s/1Dzbv8gPUZJ3T8Fe02hOJvg 提取码:zbgt py文件 from flask import Flask, j ...