zoukankan      html  css  js  c++  java
  • 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个零下时用的轮胎个数。接下来是每段路的温度,问至少要更换多少次轮胎才能到达目的地。
    题解:贪心即可,尽量少换轮胎,如果都是温度零上,就不需要更换轮胎。

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    int main()
    {
        int n,k,a[200008],i;
        while(scanf("%d%d",&n,&k)!=EOF)
        {
            int vis[200008];
            memset(vis,0,sizeof(vis));
            int flag=0;
            a[0]=1;
            int change=0;
            for(i=1; i<=n; i++)
            {
                scanf("%d",&a[i]);
                if(a[i]<0)
                {
                    flag++;
                }
                if((a[i-1]>=0&&a[i]<0)||(a[i-1]<0&&a[i]>=0))
                {
                    change++;
                }
            }
            if(flag==0)
            {
                printf("0
    ");
                continue;
            }
            if(flag>k)
            {
                printf("-1
    ");
                continue;
            }
            int j=0;
            for(i=1; i<=n; i++)
            {
                vis[j]=a[i]>=0?vis[j]+1:vis[j]-1;
                if((a[i]<0&&a[i+1]>=0)||(a[i]>=0&&a[i+1]<0))
                {
                    j++;
                    if(i==n)j--;
                }
            }
    //        for(i=0;i<=j;i++)
    //        {
    //            printf("%d %d
    ",vis[i],i);
    //        }
            if(j>1)
            {
                sort(vis+1,vis+j-1);
            }
            k=k-flag;
            for(i=1;i<j;i++)
            {
                if(vis[i]>0&&vis[i]<=k)
                {
                    change-=2;
                    k-=vis[i];
                }
            }
            if(vis[j]<=k&&vis[j]>0)
            {
                change--;
                k=k-vis[j];
            }
    //            if((a[i-1]>=0&&a[i]<0)||(a[i-1]<0&&a[i]>=0))
    //            {
    //                change++;
    //            }
            printf("%d
    ",change);
        }
    }
    
  • 相关阅读:
    水晶苍蝇拍:从“航空母舰”看企业竞争优势分析 (2010-05-11 11:48:38)
    水晶苍蝇拍:为何设定了安全边际后还吃大跌?
    水晶苍蝇拍:“低风险,高不确定性”的启示 (2010-04-24 22:02:13)
    水晶苍蝇拍:我这样看投资的安全性 (2009-08-27 20:08:53)
    水晶苍蝇拍:不同企业的估值差告诉我们什么? (2010-04-21 20:56:19)
    水晶苍蝇拍:估值,像雾像雨又像风 (2010-03-15 10:44:16)
    水晶苍蝇拍:长持的简单逻辑 (2009-05-25 18:08:43)
    Android中RelativeLayout各个属性的含义
    有道词典 Andriod 版本数据格式分析
    电驴提示“该内容尚未提供权利证明,无法提供下载”之解决办法详解
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11794324.html
Copyright © 2011-2022 走看看