codeforces 747D. Winter Is Coming(贪心)
题目链接:http://codeforces.com/problemset/problem/747/D
题意:冬天有n天,冬天用的轮胎总共能用k天,一开始车子用的是夏天的轮胎。
给出n天的平均气温,温度小于0的时候必须换上冬天用的轮胎,冬天用的轮胎
任何温度下都可以使用,问最少交换机次轮胎。
首先要找到第一个温度小于0的日子,那天肯定要换上冬天用的轮胎,然后再找
最后一个温度小于0的日子,因为在那之后就用不到冬天用的轮胎了。设最早小
于0的日子为sta,最后为end。
大情况稍微分一下
设温度小于0的天数为count
1)count = 0
输出0.
2)count > k
输出-1
3)0 < count <= k 时要分多种情况。
1.k >= n - sta + 1
输出1
2.sta - end + 1 <= k < n - sta + 1
输出2
3.k < sta - end + 1
这是要考虑如何在sta~end区间的换一下夏天用的轮胎来为冬天的轮胎续一下。
于是我们可以找sta~end之间的大于0的区间交换2次,由于我们要尽可能的少
的交换轮胎,所以我们可以找尽可能区间长的区间来换这样省了冬天轮胎使用的
天数,这里设sta~end之间减去删掉的区间总长度剩下的长度为gg,gg<=k时
就可以不用再减了,end~n的这段时间在比较一下gg+n-end与k。
1)).gg + n - end >= k
不用再加了。
2)).gg + n - end < k
再加一次
最后输出天数即可
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int M = 2e5 + 10;
int a[M] , b[M];
bool cmp(int x , int y) {
return x > y;
}
int main() {
int n , k;
cin >> n >> k;
int count = 0;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i];
if(a[i] < 0) {
count++;
}
}
if(count > k) {
cout << -1 << endl;
}
else {
int sta = 1 , end = n;
for(int i = 1 ; i <= n ; i++) {
sta = i;
if(a[i] < 0) {
break;
}
}
for(int i = n ; i >= 1 ; i--) {
end = i;
if(a[i] < 0) {
end = i;
break;
}
}
if(sta > end || count == 0) {
cout << 0 << endl;
}
else {
int cnt = end - sta + 1;
int temp = 0;
int sum = 0;
for(int i = sta ; i <= end ; i++) {
if(a[i] < 0 && sum > 0) {
b[temp++] = sum;
sum = 0;
}
if(a[i] >= 0) {
sum++;
}
}
sort(b , b + temp , cmp);
if(cnt > k) {
int gg = cnt;
int num = 0;
for(int i = 0 ; i < temp ; i++) {
gg -= b[i];
num++;
if(gg <= k)
break;
}
gg += (n - end);
if(gg > k) {
cout << 2 + num * 2 << endl;
}
else {
cout << 1 + num * 2 << endl;
}
}
else {
if(k >= n - sta + 1) {
cout << 1 << endl;
}
else {
cout << 2 << endl;
}
}
}
}
return 0;
}
codeforces 747D. Winter Is Coming(贪心)的更多相关文章
- CodeForces 747D Winter Is Coming
贪心. 只考虑负数的位置,先填间隔较小的,再填间隔较大的.如果填不满就不填,如果有多余就留给最后一个负数到终点这段路. #include<cstdio> #include<cstri ...
- codeforces Gym 100338E Numbers (贪心,实现)
题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...
- CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)
赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...
- [Codeforces 1214A]Optimal Currency Exchange(贪心)
[Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...
- Codeforces 747D:Winter Is Coming(贪心)
http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...
- 【23.26%】【codeforces 747D】Winter Is Coming
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- codeforces 349B Color the Fence 贪心,思维
1.codeforces 349B Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...
- Codeforces Gym 100269E Energy Tycoon 贪心
题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...
- CodeForces 797C Minimal string:贪心+模拟
题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...
随机推荐
- maven添加oracle驱动包
问题描述 项目用到了oracle,但由于oracle商业版权问题,maven在中心资源库直接下载jar包是要收费的 解决方法 第一步: 下载ojdbc6.jar 第二步: 将下载的jar放入项目的li ...
- 数字麦克风PDM信号采集与STM32 I2S接口应用
数字麦克风采用MEMS技术,将声波信号转换为数字采样信号,由单芯片实现采样量化编码,一般而言数字麦克风的输出有PDM麦克风和PCM麦克风,由于PDM麦克风结构.工艺简单而大量应用,在使用中要注意这二者 ...
- 0x03 前缀和与差分
前缀和 [例题]BZOJ1218 激光炸弹 计算二位前缀和,再利用容斥原理计算出答案即可. #include <iostream> #include <cstdio> #inc ...
- 【转】C++文件读写详解(ofstream,ifstream,fstream)
转:http://blog.csdn.net/kingstar158/article/details/6859379 摘要:具体用法,上面链接中,文章写的很详细,讲解ofstream,ifstream ...
- kafka客户端和服务端开发(三)
前面我们已经搭建了kafka的单机和集群环境,分别写了简单的实例代码,对于代码里面使用到的参数并没有做解释.下面我们来详细说一下各个参数的作用. 1. 创建kafka生产者 kafka生产者有3个必选 ...
- 2月11日 阿里巴巴Java开发手册 读后感
该手册分为几个部分: 印象深刻的几点: (五)集合处理 2.[强制]ArrayList的subList结果不可强转成ArrayList,否则会抛出ClassCastException 异常:java. ...
- C语言tips_1 关于&& || ! 的优先级
关于&& || ! 三种操作的优先级 测试如下 简要分析 假设&&>|| 则结果为1 假设||>&& 则结果为0 结果为1 得证 & ...
- getpost请求案例
public class MainActivity extends AppCompatActivity { private ListView lv; @Override protected void ...
- 6个美观的纯CSS渐变背景代码分享(亲测有效)
样式1 background-image: linear-gradient(160deg, #b100ff 20%,#00b3ff 80%); 样式2 background-image: linear ...
- Javarscipt中数组或者字符串的随机排序方法
在日常开发中,经常会遇到随机排序的需求,思路就是利用Math.random()方法,抽取随机数,让数组中的元素进行对调: 话不多说直接上代码,方法一:基本思路就是将a中随机抽取一个元素,放入b中,再从 ...