Codeforces Round #387 (Div. 2) Winter Is Coming(贪心)

Winter Is Coming
原题链接: http://codeforces.com/problemset/problem/747/D

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·1050 ≤ 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.


题意:

一共有N天,每天的气温已经给出,对应我们有两种轮胎,一种是冬季轮胎,一种是夏季轮胎,冬季轮胎可以在任何温度下使用,但是只能一共用K天(K天不必连续);

夏季轮胎只能在温度>=0的时候使用,没有限制天数。初始的时候是夏季轮胎

对应想要知道能不能度过整个冬天,如果可以,那么输出最少交换轮胎的次数,否则输出-1.

思路:(>=0度为暖天,<0度为冷天)
1.先记录下冬天的天数,判断对于题目给定的k能不能走完冷天,不能则输出-1,能则继续下一步。(同时记录最坏情况下换胎的次数)
2.如果k足够用的话,那么走完冷天还剩下的k就可以在暖天继续走,那么这样就可以利用贪心的思想来减少换胎的次数了。我们可以记录下前后冷天的间隔天数,当k大于它时,那么换胎次数就减少了两次。(注意要特判下最后一天)

(注意都为暖天时,就因为这个wrong了好久 )。
代码:
#include
#include
const int maxn=200000+10;
using namespace std;
int a[maxn],b[maxn];
struct node
{
    int x,y;
} s[maxn];
int main()
{
    int n,m,i;
    scanf("%d%d",&n,&m);
    for(i=0; i=0&&a[i-1]<0)||(a[i-1]>=0&&a[i]<0))//最坏的情况下需要换胎的次数
                ans++;
        }
        else if(a[0]<0)//注意冬天的第一天用的是夏胎
            ans++;
    }
    if(k==0)//注意k=0时要特判
        printf("0\n");
    else if(k>m)
        printf("-1\n");
    else if(k==m)
        printf("%d\n",ans);
    else
    {
        k=m-k;//多余的冬胎使用天数
        int cnt=0,t=0;
        for(i=0; i=0)//特判下最后一天
        {
            b[t]=n-1-s[cnt-1].y;
            if(b[t]<=k)
                ans-=1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

你可能感兴趣的:(贪心,ACM)