Codeforces Round #387 (Div. 2)D. Winter Is Coming(优先队列)

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.

题意:冬天轮胎能在任何平均温度下行驶k天,夏天轮胎能在非负平均温度下行驶任意天,现在给出n天数和冬天轮胎可行驶k天,问行驶n天至少更换轮胎多少次。

思路:至少我们知道温度是负的时候肯定要用到冬天轮胎,先假设温度非负时候用夏天轮胎,而只有温度为负数的时候才用冬天轮胎,这样算出来的更换次数是最多的。

现在把温度非负的区间一个一个地拿出来(除了一开始的那块),现在把区间从小到大排个序,把剩余的k花在这些区间内,每填满一个区间更换次数就可以减少2,如果这个区间是i...n,那就只能减少一次,这时候要比较最后一块填和不填哪个更优。(就差一点点......)

#include 
using namespace std;
#define ll long long int
#define maxn 200010
struct node{
    int len, l, r;
    bool operator<(const node &a)const{
        return len>a.len;
    }
};
int day[maxn];
int m[maxn];
int main()
{
    int n, k, c, i, j;
    scanf("%d %d", &n, &k);
    c = 0;
    for(i = 0;i < n;i++){
        scanf("%d", &day[i]);
        if(day[i]<0) c++;
    }
    if(c>k){
        printf("-1\n");
        return 0;
    }
    int turn = 0;
    bool f = 0;
    priority_queue q;

    for(i = 0;i < n;i++){
        if(day[i]<0){
            m[i] = 1;
            k--;
        }
        if(!f&&day[i]<0){
            turn++;
            f = 1;
        }
        if(f&&day[i]>=0){
            turn++;
            f = 0;
        }
    }
    int l = 0, r = 0;
    i = 0;
    while(m[i]==0&&i=x.len){
                k-=x.len;
                if(x.r!=n-1) {
                    turn -=2;
                }else turn--;
            }
            if(kk>=x.len){
                if(x.r!=n-1) {
                    kk-=x.len;
                    tt -= 2;
                }
            }
        }
    }
    printf("%d\n", min(turn, tt));
}

你可能感兴趣的:(codeforces,优先队列,codeforces,优先队列)