CF747D.Winter Is Coming
普及+/提高
通过率:0%
时间限制:1.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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.
Berland 的冬季持续 n 天。对于每一天,我们已知当天的平均气温预报值。
Vasya 有一套全新的冬季轮胎,该套轮胎在任意平均气温下均最多可安全使用 k 天。在累计使用 k 天后(无论这些天的气温如何),该套冬季轮胎即告磨损,无法继续使用。这 k 天不必是连续的。
在第一个冬季日之前,Vasya 仍在使用夏季轮胎。当平均气温为非负数时,夏季轮胎可安全行驶任意天数;当平均气温为负数时,则无法使用夏季轮胎行驶。
Vasya 可在任意一天的开始时刻更换轮胎:即把夏季轮胎换成冬季轮胎,或把冬季轮胎换成夏季轮胎。
请找出 Vasya 在整个冬季期间为保证安全行驶所需进行的最少轮胎更换次数(夏季 ↔ 冬季)。冬季结束时,汽车可以装有任意一种轮胎。
输入格式
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 _t_1, _t_2, ..., t__n ( - 20 ≤ t__i ≤ 20) — the average air temperature in the i-th winter day.
第一行包含两个正整数 n 和 k(1≤n≤2⋅105,0≤k≤n)—— 分别表示冬季天数和冬季轮胎最多可使用的天数。允许在任意温度下使用冬季轮胎,但总使用天数不得超过 k 天。
第二行包含一个由 n 个整数组成的序列 t1,t2,...,tn(−20≤ti≤20)—— 表示第 i 个冬季天的平均气温。
输出格式
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.
输出瓦夏在整个冬季安全驾驶所需更换夏季轮胎与冬季轮胎(两者相互更换)的最少次数。如果无法实现,输出 −1。
输入输出样例
输入#1
4 3 -5 20 -3 0
输出#1
2
输入#2
4 2 -5 20 -3 0
输出#2
4
输入#3
10 6 2 -5 1 3 0 0 -4 -3 1 0
输出#3
3
说明/提示
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.
在第一个例子中,瓦西娅应在第一个冬日之前将夏季轮胎更换为冬季轮胎,使用三天后,再将冬季轮胎更换为夏季轮胎,因为冬季轮胎仅能安全行驶三天。因此,轮胎更换总次数为两次。
在第二个例子中,瓦西娅应在第一个冬日之前将夏季轮胎更换为冬季轮胎,然后在第一个冬日之后将冬季轮胎更换为夏季轮胎;在第二个冬日之后需再次将夏季轮胎更换为冬季轮胎,在第三个冬日之后又需将冬季轮胎更换为夏季轮胎。因此,轮胎更换总次数为四次。
输入解题思路,AI测评打分。不知道怎么写?