Codeforces Round #387 (Div. 2) D && coedeforces 744 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. Afterk 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.


题意:冬天轮胎能在任何平均温度下行驶k天,夏天轮胎能在非负平均温度下行驶任意天,现在给出n天数和冬天轮胎可行驶k天,问行驶n天至少更换轮胎多少次。
思路:,我们知道夏天的轮胎只能在夏天用,冬天的轮胎也可以用在夏天,那么也就是说:对于给定的冬天轮胎可使用的天数,我们先找出其中是冬天的天数,如果小于冬天的天数那么直接输出-1;
那么我们先假设温度非负时候用夏天轮胎,而只有温度为负数的时候才用冬天轮胎,这样算出来的更换次数是最多的。
其次,如果可使用的冬天数足够的话,我们为了减少换轮胎的次数,我们就将剩下的天数按照贪心的思想填在两个冬天之间的地方,也就是说如果剩余的天数可以覆盖两个冬天之间这些天,那么我们可以不用换轮胎直接行使,因为从夏天到冬天在到夏天需要更换次数为2,这样我们就可以 减少两次 换轮胎的次数,所以尽量使两个之间的差小,这样就-的就少了,判断下最后还有没有剩余的可以用夏天的,并且生下的冬天轮胎可以支持这么多天。。最后的是-1

思路知道了之后,写的时候 还是wa了好几次 ,代码能力,代码能力啊。。。潘老师10分钟就可以了。。。可能还是写的时候,思路不清晰吧,要锻炼写代码的时候思路一致时清晰的啊

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn = 5e6 + 5;
int a[maxn];
int main()
{
    int n, k, p;
    cin >> n >> k;
    int pre = -1;
    priority_queue, greater > q;
    int cnt = 0;
    for(int i = 1; i <= n; i++)
    {
        cin >> a[i];
        if(a[i] < 0 && pre != -1)
        {
            if(i-pre-1 != 0)  //相邻的话 中间就没有夏天的了,不能这样贪心
            q.push(i-pre-1);
            pre = i;
            cnt++;

        }
        if(a[i] < 0 && pre == -1)
            pre = i, cnt++;
    }
    if(cnt > k)
    {
        cout << -1 << endl;
        return 0;
    }
    p = 1;
    int ans = 0;
    for(int i = 1; i <= n; i++)
    {
        if(p*a[i] < 0)
            p = -p, ans++;
        else if(p == -1 && a[i] == 0)
            p = 1, ans++;
    }
    k = k - cnt;
    while(q.size())
    {
        if(k >= q.top())
            k -= q.top(), ans -= 2, q.pop();
        else
            break;
        if(k == 0)
            break;
    }
    if(k >= n-pre && n-pre != 0) ans--;  //保证最后一个不是冬天的
        cout << ans << endl;
    return 0;
}



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