贪心 CodeForces - 747D 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 thank 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 thesek 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 isnon-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 thank 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.


题意:夏天轮胎只能在温度>=0时走,冬天轮胎能在任何天气走,但冬天轮胎只能用k天。一开始车装的是夏胎,给定每天温度,问最少需要换多少次胎。

思考:1、最坏情况:冬夏轮流交替,这时每天都需要换一次轮胎;

  2、为了使换胎次数尽量少,最好使冬天连续。

  3、最后一次换冬胎后,剩余的使用天数能否维持到结束?

#include <iostream>
#include <cstdio>
#include <algorithm>

using namespace std;
struct node{
    int x, y;
}s[2*100000 + 10];
int t[2*100000 + 10], d[2*100000 + 10];

int main()
{
    int n, k, num, g, ans, cnt;
    num = 0, ans = 0;
    scanf("%d%d", &n, &k);  //天数n, 冬胎耐久度k
        for(int i = 1; i <= n; i++){
            scanf("%d", &t[i]);

            if(t[i] < 0)
                num++;  // 冬天天数
            if(i-1){
                if( (t[i] < 0 && t[i-1] >= 0) || (t[i] >= 0 && t[i-1] < 0) )
                    ans++;  // 最坏情况
            }
        }
        if(t[1] < 0) ans++;  // 判断第一天天气决定第一天是否需要换胎
        if(num == 0){  // 如果没有冬天直接输出0
            printf("0\n");
        }else if(num > k) {  // 冬天天数比耐久度大,直接输出-1
            printf("-1\n");
        }else if(num == k){  // 相同直接输出换胎次数
            printf("%d\n", ans);
        }else{
            k = k - num;  // 用完冬天后剩余耐久度
            int cnt = 0, g = 0;
            for(int i = 1; i <= n; i++){
                if(t[i] < 0){
                    int j = i;
                    while(t[j] < 0 && j <= n)
                        j++;
                    s[cnt].x = i, s[cnt].y = j-1;  // x是每段连续冬天的开始时间, y是结束时间
                    if(cnt)
                        d[g++] = s[cnt].x - s[cnt-1].y - 1; // 从第二段开始统计每段之间的间隔天数
                    i = j-1;
                    cnt++;
                }
            }
            sort(d, d+g);  //对间隔天数排序, 优先填补能连上的两段间隔使其变成一段
            for(int i = 0; i < g; i++){
                if(d[i] <= k){
                    k -= d[i];
                    ans -= 2;  // 一旦填上一段,换胎次数就可以减少两次
                }
                else break;  // 耐久度再也无法用于填补
            }
            // 判断最后一次换胎后耐久度能否用到结束
            if(t[n] >= 0){
                d[g] = n - s[cnt-1].y;
                if(d[g] <= k)
                    ans -= 1;
            }
            printf("%d\n", ans);
        }
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值