http://codeforces.com/problemset/problem/747/D

题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意。问最少的换胎次数。

思路:先数冬天的天数tol,如果天数>k的话,那么就不可能度过。否则,最坏情况下,每到冬天就换一次冬天轮胎,然后度过冬天就换夏天轮胎,所以答案ans = 2*tol。然后考虑尽量让每段冬天连续,这样可以减少换胎次数,于是算出冬天之间的间隔,然后从小到大排序,每次减少一段间隔,ans就可-2。然后考虑特殊情况,如果最后一次换冬天轮胎,可以用到结束,那么ans-1。

 #include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
#define N 200010
typedef long long LL;
vector<int> vec;
int t[N];
int main() {
int n, k;
cin >> n >> k;
int tol = , ans = ;
int pre = -, last = -;
for(int i = ; i <= n; i++) {
scanf("%d", &t[i]);
if(t[i] < ) {
last = i; // 最后一次冬天
ans += ;
tol++;
if(pre != -)
vec.push_back(i - pre - ); // 处理冬天间隔
pre = i;
}
} k -= tol;
if(k < ) {
puts("-1");
} else {
sort(vec.begin(), vec.end());
for(int i = ; i < vec.size(); i++) {
if(k - vec[i] >= ) {
k -= vec[i];
ans -= ;
} else break;
}
if(n - last <= k) ans--;
printf("%d\n", ans);
}
return ;
}

Codeforces 747D:Winter Is Coming(贪心)的更多相关文章

  1. codeforces 747D. Winter Is Coming(贪心)

    题目链接:http://codeforces.com/problemset/problem/747/D 题意:冬天有n天,冬天用的轮胎总共能用k天,一开始车子用的是夏天的轮胎. 给出n天的平均气温,温 ...

  2. CodeForces 747D Winter Is Coming

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

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

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

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

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

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

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

  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. 一些稍微复杂点的sql语句

    UPDATE test SET content = REPLACE(content,'国家级',''),content = REPLACE(content,'世界级',''),content = RE ...

  2. redis原子性读写操作之LUA脚本和watch机制

    最近在开发电商平台的子系统--储值卡系统,系统核心业务涉及到金额消费以及库存控制,因此为了解决建立在内存上高并发情况下的事务控制,使用了spring封装的RedisTemplate执行lua脚本进行原 ...

  3. 1JavaScript简介

    文档对象模型(DOM,Document Object Model)是针对XML但经过扩展用于HTML的应用程序编程接口(API,Application Programming Interface). ...

  4. css基础知识

    CSS 规则由两个主要的部分构成:选择器,以及一条或多条声明.其中选择器通常是您需要改变样式的 HTML 元素(比如p标签),也可以是节点的属性的值(比如id,class):每条声明都是一条字典key ...

  5. C++学习笔记 知识集锦(二)

    1. 命名规范 2. 代码格式 3. QString的判断 4. 对象的判空 5. 隐式接口&显式接口 6. vector&string 7. static 8. const 9. v ...

  6. SQL Server 不清空数据,修改数据库字段、结构,阻止保存要求重新创建表的更改

    当数据库有数据修改数据库字段时,默认是阻止的! 工具---选项---设计器---阻止保存要求重新创建表的更改(取消钩)

  7. AFNETWorking 不支持中文URL请求

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px Menlo; color: #000000; min-height: 16.0px } p.p ...

  8. App.domain http->https

    App.domain = `${location.origin}/toa-mgw/rest/gateway`.replace(/http:\/\//, 'https://');

  9. FreeBSD 查看硬件信息

    systat 能实时查看各种信息 systat -pigs 默认值CPU systat -iostat 硬盘IO systat -swap 交换分区 systat -mbufs 网络缓冲区 systa ...

  10. ubuntu command

    uninstall software: sudo apt-get purge openjdk*