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的末尾 ...
随机推荐
- jenkins弱口令漏洞
jenkins弱口令漏洞 一.漏洞描述 通过暴力破解管理控制台,如果爆破成功,可获得后台管理权限.操作后台,后台可通过脚本命令行功能执行系统命令,如反弹shell等,低权限可以通过创建控制台输出方式执 ...
- 基于 Autojs 的 APP、小程序自动化测试 SDK - 2019年8月3日
原文:https://blog.csdn.net/laobingm/article/details/98317394 autojs sdk基于 Autojs 的 APP.小程序自动化测试 SDK,支持 ...
- ceph log机制
Log 是每个项目必须的,他是跟踪问题的最直接的依据.Ceph 也设计自己的log机制. 初始化启动log实例,启动log线程. _log = new ceph::log::Log(&_con ...
- LeetCode :1.两数之和 解题报告及算法优化思路
最近开始重拾算法,在 LeetCode上刷题.顺便也记录下解题报告以及优化思路. 题目链接:1.两数之和 题意 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 ...
- 利用ImageAI库只需几行python代码超简实现目标检测
目录 什么是目标检测 目标检测算法 Two Stages One Stage python实现 依赖 安装 使用 附录 什么是目标检测 目标检测关注图像中特定的物体目标,需要同时解决解决定位(loca ...
- Linux常用命令之ftp
FTP是Internet用户使用最频繁的文件上传.下载的命令之一.linux ftp用命令的方式来控制在本机和远程ftp服务器之间传送文件.ftp中的命令包括上传文件(单个.多个),下载文件(单个.多 ...
- XML简单了解一下
XML是一种纯文本文档.HTML,标记是已经被W3C规定好的,自己创建一个标签是不被允许的. XML现在的用途是用来存储数据.config文件就是个XML文档.XML是可以自定义的. 每一个XML文档 ...
- python骚操作---Print函数用法
---恢复内容开始--- python骚操作---Print函数用法 在 Python 中,print 可以打印所有变量数据,包括自定义类型. 在 3.x 中是个内置函数,并且拥有更丰富的功能. 参数 ...
- spring boot 打 war包
spring boot .spring cloud打 war包,并发布到tomcat中运行 1.pom文件修改 <packaging>war</packaging> 2.< ...
- BUPTOJj83
83. A + B Problem 时间限制 1000 ms 内存限制 65536 KB 题目描述 Calculate the sum of two given integers A and B. 输 ...