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的末尾 ...
随机推荐
- 基于 Autojs 的 APP、小程序自动化测试 SDK - 2019年8月3日
原文:https://blog.csdn.net/laobingm/article/details/98317394 autojs sdk基于 Autojs 的 APP.小程序自动化测试 SDK,支持 ...
- python 获取大乐透中奖结果
实现思路: 1.通过urllib库爬取http://zx.500.com/dlt/页面,并过滤出信息 2.将自己的买的彩票的号与开奖号进行匹配,查询是否中奖 3.将中奖结果发生到自己邮箱 caipia ...
- jQuery发送Ajax请求以及出现的问题
普通jQuery的Ajax请求代码如下: $.ajax({ type: 'POST', url: "http://xxx/yyy/zzz/sendVerifyCode", data ...
- WebService1
一.什么是WebService(来源百度百科) Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述. ...
- LeetCode——372. Super Pow
题目链接:https://leetcode.com/problems/super-pow/description/ Your task is to calculate ab mod 1337 wher ...
- ssm访问不了后台
最近整理ssm,写完demo案例,无论如何都访问不了后台,百度了好多,终于解决了问题所在 先看页面信息: 因为一直报404错误,一直找路径是不是弄错了,或配置文件弄错了,仅仅这个配置文件都看了无数遍, ...
- JavaWeb前端分页显示方法
在前端中我们总会遇到显示数据的问题 - 正常情况分页显示是必须的,这个时候我们不能仅仅在前端进行分页,在前端其实做起分页是很困难的,着就要求我们在后台拿数据的时候就要把分页数据准备好,在前端我们只需要 ...
- studio无限轮播
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...
- node一键发布,并运行
作为一个前端开发人员如果你只会写一些业务代码,从程序员的角度来考虑已经可以了.但是从架构的角度来考虑那远远不够: 在此记录下成长中的经历: 想要达成的目的:运行一个脚本实现代码的打包,上传至服务器并部 ...
- 天天都用消息队列,却不知道为啥要用MQ,这就有点尴尬了
1.为什么要使用消息队列? 分析:一个用消息队列的人,不知道为啥用,有点尴尬.没有复习这点,很容易被问蒙,然后就开始胡扯了. 回答:这个问题,咱只答三个最主要的应用场景(不可否认还有其他的,但是只答三 ...