zoukankan      html  css  js  c++  java
  • CF2.D

    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.

     题意:

    有一个冬季轮胎,和一个夏季轮胎,夏季轮胎只能在温度大于的等于0时才能用并且使用寿命无限长,冬季轮胎任何温度下都可以用,但有一个使用寿命k,只能用k天。给出n天的温度,问最少更换几次轮胎。开始时汽车用的是夏季轮胎。

    代码:

     1 //比较水,比赛时没敢写。先找出那些天的温度小于零,然后看每两个小于零之间的天中用冬季轮胎能否够用,
     2 //如果够用更换次数就减2,如果最后一个温度小于零的天不是最后一天,若他后面的几天都用冬季轮胎更换次数减1.
     3 #include<bitsstdc++.h>
     4 using namespace std;
     5 typedef long long ll;
     6 bool cmp(int x,int y)
     7 {
     8     return x<y;
     9 }
    10 int main()
    11 {
    12     int n,k,a[200005],b[200005];
    13     cin>>n>>k;
    14     int cnt=0,la=0,K=k;
    15     for(int i=1;i<=n;i++)
    16     {
    17         cin>>a[i];
    18         if(la!=0&&a[i]<0)
    19         {
    20             b[++cnt]=i-la-1;
    21             k--;
    22             la=i;
    23         }
    24         else if(la==0&&a[i]<0)
    25         {
    26             k--;
    27             la=i;
    28         }
    29     }
    30     if(k<0) cout<<"-1
    ";
    31     else if(la==0) cout<<"0
    ";
    32     else
    33     {
    34         int ans=(K-k)*2;
    35         if(a[n]<0)
    36         ans--;
    37         int sum=k;
    38         sort(b+1,b+1+cnt,cmp);
    39         for(int i=1;i<=cnt;i++)
    40         {
    41             if(sum>=b[i])
    42             {
    43                 sum-=b[i];
    44                 ans-=2;
    45             }
    46         }
    47         if(a[n]>=0)
    48         {
    49             if(sum>=n-la) ans--;
    50         }
    51         cout<<ans<<endl;
    52     }
    53     return 0;
    54 }
  • 相关阅读:
    Sublime Text 3——插件配置篇
    Sublime Text 3——基本介绍篇
    线性同余方程
    费马小定理
    一点心事
    寒诗
    e网通学习笔记
    std::cout<<"Goodbye 2019"<<" "<<"Hello 2020"<<' ';
    新砍
    NOIP2019游记
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6201740.html
Copyright © 2011-2022 走看看