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

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.

题意:给定N天的气温,小于零的温度认为是低温,反之为高温。冬季轮胎最多可以安全使用K天,但也可以在高温天气下实用。夏季轮胎没有使用天数限制,但只能在高温天气使用。进入第一天之前是夏季轮胎,在进入每天之前都可以选择瞬间更换轮胎。求出安全行驶完N天需要更换轮胎的最小次数。

思路:记录下低温天气的总天数为winter,如果K

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<queue>
#include<math.h>
#include<string>
using namespace std;
const int maxn=100005*2;
int num[maxn];
int result[maxn];
int main()
{
    int N,K;
    scanf("%d%d",&N,&K);
    int Day=0;
    for(int i=0; i<N; i++)
    {
        scanf("%d",&num[i]);
        if(num[i]<0)
            Day++;
    }
    if(Day>K)
    {
//        printf("%d**%d**\n",Day,K);
        printf("-1\n");
        return 0;
    }
    Day=K-Day;
    long long int result_num=0;//低温区域数量
    for(int i=0; i<N; i++)
    {
        if(num[i]<0)
        {
            while(num[i]<0&&i<N)
                i++;
            i--;
            result_num++;
        }
    }
    result_num=result_num*2;
    int left=0,right=0,len=0;//高温区域左右边界及数量
    while(right<N)
    {
        while(num[right]>=0&&right<N)
            right++;
        if(right>left)
            result[len++]=right-left;
        while(num[right]<0&&right<N)
            right++;
        left=right;
    }
    if(len==1&&result[0]==N)//N个正数
    {
        printf("0\n");
        return 0;
    }

    if(num[N-1]>=0)//如果尾部是高温
    {
        if(num[0]>=0)//如果头部是高温
        {
            if(len>2)//如果掐头去尾后还有数需要排序
                sort(result+1,result+len-1);//最左边的高温区域不用管
            for(int i=1; i<len-1; i++)//最右边的高温区域特判
            {
                if(result[i]<=Day)
                {
                    result_num-=2;
                    Day-=result[i];
                }
                else
                    break;
            }
            if(result[len-1]<=Day)
            {
                result_num--;
                printf("%I64d\n",result_num);
            }
            else
                printf("%I64d\n",result_num);
        }
        else//头部是低温
        {
            if(len>1)//如果去尾后还有数需要排序
                sort(result,result+len-1);
//            printf("%d**\n",len);
            for(int i=0; i<len-1; i++)//最右边的高温区域特判
            {
//                printf("%d***\n",result[i]);
                if(result[i]<=Day)
                {
                    result_num-=2;
                    Day-=result[i];
                }
                else
                    break;
            }
            if(result[len-1]<=Day)
            {
                result_num--;
                printf("%I64d\n",result_num);
            }
            else
                printf("%I64d\n",result_num);
        }
    }
    else//尾部是低温
    {
        if(num[0]>=0)//如果头部是高温
        {
            if(len>1)//如果掐头后还有数需要排序
                sort(result+1,result+len);//最左边的高温区域不用管
            for(int i=1; i<len; i++)
            {
                if(result[i]<=Day)
                {
                    result_num-=2;
                    Day-=result[i];
                }
                else
                    break;
            }
            printf("%I64d\n",result_num-1);
        }
        else//如果头部是低温
        {
            if(len>0)
                sort(result,result+len);
            for(int i=0; i<len; i++)
            {
                if(result[i]<=Day)
                {
                    result_num-=2;
                    Day-=result[i];
                }
                else
                    break;
            }
            printf("%I64d\n",result_num-1);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值