博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CF2.D
阅读量:5220 次
发布时间:2019-06-14

本文共 3757 字,大约阅读时间需要 12 分钟。

D. Winter Is Coming
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

The winter in Berland lasts n days. For each day we know the forecast for the average air temperature that day.

Vasya has a new set of winter tires which allows him to drive safely no more than k days at any average air temperature. After k days of using it (regardless of the temperature of these days) the set of winter tires wears down and cannot be used more. It is not necessary that these k days form a continuous segment of days.

Before the first winter day Vasya still uses summer tires. It is possible to drive safely on summer tires any number of days when the average air temperature is non-negative. It is impossible to drive on summer tires at days when the average air temperature is negative.

Vasya can change summer tires to winter tires and vice versa at the beginning of any day.

Find the minimum number of times Vasya needs to change summer tires to winter tires and vice versa to drive safely during the winter. At the end of the winter the car can be with any set of tires.

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 2·105, 0 ≤ k ≤ n) — the number of winter days and the number of days winter tires can be used. It is allowed to drive on winter tires at any temperature, but no more than k days in total.

The second line contains a sequence of n integers t1, t2, ..., tn ( - 20 ≤ ti ≤ 20) — the average air temperature in the i-th winter day.

Output

Print the minimum number of times Vasya has to change summer tires to winter tires and vice versa to drive safely during all winter. If it is impossible, print -1.

Examples
input
4 3 -5 20 -3 0
output
2
input
4 2 -5 20 -3 0
output
4
input
10 6 2 -5 1 3 0 0 -4 -3 1 0
output
3
Note

In the first example before the first winter day Vasya should change summer tires to winter tires, use it for three days, and then change winter tires to summer tires because he can drive safely with the winter tires for just three days. Thus, the total number of tires' changes equals two.

In the second example before the first winter day Vasya should change summer tires to winter tires, and then after the first winter day change winter tires to summer tires. After the second day it is necessary to change summer tires to winter tires again, and after the third day it is necessary to change winter tires to summer tires. Thus, the total number of tires' changes equals four.

 题意:

有一个冬季轮胎,和一个夏季轮胎,夏季轮胎只能在温度大于的等于0时才能用并且使用寿命无限长,冬季轮胎任何温度下都可以用,但有一个使用寿命k,只能用k天。给出n天的温度,问最少更换几次轮胎。开始时汽车用的是夏季轮胎。

代码:

1 //比较水,比赛时没敢写。先找出那些天的温度小于零,然后看每两个小于零之间的天中用冬季轮胎能否够用, 2 //如果够用更换次数就减2,如果最后一个温度小于零的天不是最后一天,若他后面的几天都用冬季轮胎更换次数减1. 3 #include
4 using namespace std; 5 typedef long long ll; 6 bool cmp(int x,int y) 7 { 8 return x
>n>>k;14 int cnt=0,la=0,K=k;15 for(int i=1;i<=n;i++)16 {17 cin>>a[i];18 if(la!=0&&a[i]<0)19 {20 b[++cnt]=i-la-1;21 k--;22 la=i;23 }24 else if(la==0&&a[i]<0)25 {26 k--;27 la=i;28 }29 }30 if(k<0) cout<<"-1\n";31 else if(la==0) cout<<"0\n";32 else33 {34 int ans=(K-k)*2;35 if(a[n]<0)36 ans--;37 int sum=k;38 sort(b+1,b+1+cnt,cmp);39 for(int i=1;i<=cnt;i++)40 {41 if(sum>=b[i])42 {43 sum-=b[i];44 ans-=2;45 }46 }47 if(a[n]>=0)48 {49 if(sum>=n-la) ans--;50 }51 cout<
<

 

转载于:https://www.cnblogs.com/--ZHIYUAN/p/6201740.html

你可能感兴趣的文章
路冉的JavaScript学习笔记-2015年1月23日
查看>>
Mysql出现(10061)错误提示的暴力解决办法
查看>>
2018-2019-2 网络对抗技术 20165202 Exp3 免杀原理与实践
查看>>
NPM慢怎么办 - nrm切换资源镜像
查看>>
CoreData 从入门到精通(四)并发操作
查看>>
Swift - UIView的常用属性和常用方法总结
查看>>
Swift - 异步加载各网站的favicon图标,并在单元格中显示
查看>>
Java编程思想总结笔记Chapter 5
查看>>
[LeetCode]662. Maximum Width of Binary Tree判断树的宽度
查看>>
WinForm聊天室
查看>>
【Python学习笔记】1.基础知识
查看>>
梦断代码阅读笔记02
查看>>
selenium学习中遇到的问题
查看>>
大数据学习之一——了解简单概念
查看>>
Linux升级内核教程(CentOS7)
查看>>
Lintcode: Partition Array
查看>>
分享适合个人站长的5类型网站
查看>>
类别的三个作用
查看>>
【SICP练习】85 练习2.57
查看>>
runC爆严重安全漏洞,主机可被攻击!使用容器的快打补丁
查看>>