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的末尾 ...
随机推荐
- UE4 打包详细流程
这两天试着把之前做的一个UE4项目在安卓机上运行下,于是乎有了下面的一个打包血泪史. 首先呢,肯定是下载好了UE的源码了,我用的是4.18. 安装步骤可以先参考下官方的教程http://api.unr ...
- golang文档、中文、学习文档
Golang中文文档地址 http://zh-golang.appspot.com/doc/ Golang非英文文档地址: https://github.com/golang/go/wiki/NonE ...
- RocketMQ中Broker的启动源码分析(一)
在RocketMQ中,使用BrokerStartup作为启动类,相较于NameServer的启动,Broker作为RocketMQ的核心可复杂得多 [RocketMQ中NameServer的启动源码分 ...
- 运营商手机视频流量包业务日志ETL及统计分析
自己做过的项目在这里做一个记录,否则就感觉不是自己的了.一是因为过去时间已经很长了,二是因为当时做得有点粗糙,最后还不了了之了. 话不多说,先大致介绍一下项目背景.以前各大手机视频 App 一般都有运 ...
- React 基于antd+video.js实现m3u8格式视频播放及实时切换
文档连接地址(官网看起麻烦,看中文别人整理好的)https://blog.csdn.net/a0405221/article/details/80923090 React项目使用 安装依赖 npm ...
- SpringBoot:如何优雅地处理全局异常?
之前用springboot的时候,只知道捕获异常使用try{}catch,一个接口一个try{}catch,这也是大多数开发人员异常处理的常用方式,虽然屡试不爽,但会造成一个问题,就是一个Contro ...
- java虚拟机学习笔记(四)---回收方法区
Java虚拟机规范中规定不要求虚拟机在方法区实现垃圾收集,而且在方法区实现垃圾收集性价比确实很低.在堆中,尤其是新生代,一次垃圾收集可以回收75%-95%的空间,而永久代的垃圾回收效率远低于此. 永久 ...
- coursera课程《how to learning 怎么学习》 总结
总体来说,学完课程没有茅舍顿开的感觉,而是更加印证了之前的那个认知:大道至简,践则无敌,很多的学习方法上学的时候老师都教过我们,关键是我们能否坚持执行.课程讲了很多脑科学有关学习的知识,但对于我们实践 ...
- Windows下的bat原来可以为我们做很多
用了windows系统这么多年了,对bat也不是很了解.最近研究了一下bat的用法.这里就大概列举一下自己的用法 参考网址 基本命令 echo echo我们可以理解成程序中的输出,和我们Java的Sy ...
- 8.6 day27 网络编程 osi七层协议 Time模块补充知识 TCP协议
Time模块补充知识 date和datetime区别是什么? date 就是年月日 datetime就是年月时时分秒 以下代码为什么会报错? import json from datetime imp ...