题目链接: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(贪心)的更多相关文章

  1. CodeForces 747D Winter Is Coming

    贪心. 只考虑负数的位置,先填间隔较小的,再填间隔较大的.如果填不满就不填,如果有多余就留给最后一个负数到终点这段路. #include<cstdio> #include<cstri ...

  2. codeforces Gym 100338E Numbers (贪心,实现)

    题目:http://codeforces.com/gym/100338/attachments 贪心,每次枚举10的i次幂,除k后取余数r在用k-r补在10的幂上作为候选答案. #include< ...

  3. CodeForces 839D - Winter is here | Codeforces Round #428 (Div. 2)

    赛后听 Forever97 讲的思路,强的一匹- - /* CodeForces 839D - Winter is here [ 数论,容斥 ] | Codeforces Round #428 (Di ...

  4. [Codeforces 1214A]Optimal Currency Exchange(贪心)

    [Codeforces 1214A]Optimal Currency Exchange(贪心) 题面 题面较长,略 分析 这个A题稍微有点思维难度,比赛的时候被孙了一下 贪心的思路是,我们换面值越小的 ...

  5. Codeforces 747D:Winter Is Coming(贪心)

    http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...

  6. 【23.26%】【codeforces 747D】Winter Is Coming

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. codeforces 349B Color the Fence 贪心,思维

    1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1 ...

  8. Codeforces Gym 100269E Energy Tycoon 贪心

    题目链接:http://codeforces.com/gym/100269/attachments 题意: 有长度为n个格子,你有两种操作,1是放一个长度为1的东西上去,2是放一个长度为2的东西上去 ...

  9. CodeForces 797C Minimal string:贪心+模拟

    题目链接:http://codeforces.com/problemset/problem/797/C 题意: 给你一个非空字符串s,空字符串t和u.有两种操作:(1)把s的首字符取出并添加到t的末尾 ...

随机推荐

  1. ubuntu清理系统垃圾与备份

    虽然linux下不会有windows下的那么多垃圾和磁盘碎片!但还是会留下一些用不着的临时文件或是多次升级后的N个旧的内核! 1,非常有用的清理命令: sudo apt-get autoclean s ...

  2. JDK的可视化工具系列 (四) JConsole、VisualVM

    JConsole: Java监视与管理控制台 代码清单1: import java.util.*; public class JConsoleDemo { static class OOMObject ...

  3. 异步编程CompletableFuture实现高并发系统优化之请求合并

    先说场景: 根据Redis官网介绍,单机版Redis的读写性能是12万/秒,批量处理可以达到70万/秒.不管是缓存或者是数据库,都有批量处理的功能.当我们的系统达到瓶颈的时候,我们考虑充分的压榨缓存和 ...

  4. web渗透---第一天

    了解黑客     黑客: 黑客是一个中文词语,皆源自英文hacker,随着灰鸽子的出现, 灰鸽子成为了很多假借黑客名义控制他人电脑的黑客技术,于是 出现了“骇客”与黑客”分家. 黑客:Hacker 骇 ...

  5. gitee+hexo搭建个人博客

    通过gitee和hexo搭建个人博客 首先准备软件: git (提供命令git) git官网 notepad++(方便编辑)notepad++官网 nodejs(hexo依赖)nodejs官网 7z( ...

  6. IT技术管理者的自我修养

    1. 前言 本来写<IT技术管理者的自我修养>与<IT技术人员的自我修养>是一开始就有的想法.但发表<IT技术人员的自我修养>后,收到了不少良好的反馈,博客园的编辑 ...

  7. React 如何搭建脚手架

    React 如何搭建脚手架   npm install -g create-react-app    //安装 create-react-app react-demo    // react-demo ...

  8. 同时运行多个 tomcat 修改端口

    修改 tomcat 配置文件,路径: tomcat_home/conf/server.xml  1.HTTP端口,默认8080,如下改为8081 <Connector connectionTim ...

  9. Leetcode的SQL题解:185. 部门工资前三高的员工

    题目 查询部门工资前三高的员工. 我用的数据库是oracle. 下面是数据表的信息. Employee表数据: | ID | NAME | Salary | DepartmentId | | -- | ...

  10. Spark 系列(七)—— 基于 ZooKeeper 搭建 Spark 高可用集群

    一.集群规划 这里搭建一个 3 节点的 Spark 集群,其中三台主机上均部署 Worker 服务.同时为了保证高可用,除了在 hadoop001 上部署主 Master 服务外,还在 hadoop0 ...