# Problem 747D - Winter Is Coming

2016-12-31 18:51:44来源:CSDN作者:qq_36551426人点击

————————————————————————————————————————————-

**

**
**

## D. Winter Is Coming

**
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

``#include <iostream>#include <algorithm>using namespace std;const int maxn=2e5+4;int s[maxn+5];int main(){    int n,k;    int cnt=0,t,x=0;    cin>>n>>k;    for (int i=0;i<n;i++)//对n长度的串进行输入且处理    {        cin>>t;        if (t>=0) cnt++;//if-else记录了每一段正数的个数        else        {            s[x++]=cnt;//且由s数组记录，数组从0~x            cnt=0;        }    }    if (x>k) return cout<<-1,0;//此处进行特判，若x>k输出-1，结束程序    if (x==0) return cout<<0,0;//若x==0，输出0，结束程序    sort (s+1,s+x);//因为有贪心的思想存在，所以对正数长度进行排序    //另:此处有比较恶心的一点，排序是从1到x排的，而数组是从0~x，换句话说s[0]被忽略了    //至于被忽略的原因：若前方全是正数，还是要从普通状态切换到护盾,若无数字，效果一样    k-=x;    int res=2*x;    for (int i=1;i<x;i++)    {        if (k>=s[i])//此处k用于两负数之间的非负数以减少shift的次数        {            k-=s[i];            res-=2;        }    }    if (k>=cnt) res--;//对尾部正数进行判断    cout<<res<<endl;}``

PS：

无相关信息