Problem 747D - Winter Is Coming

2016-12-31 18:51:44来源:CSDN作者:qq_36551426人点击

第七城市

前两天打了一下CF,分掉的厉害,所以来写一下题解。

————————————————————————————————————————————-

原题连接:http://codeforces.com/problemset/problem/747/D

**

题目

**
**

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 天时间,而当森林平均温度为负数时,会进入weak状态,此时需要护盾支持他继续走, k 为护盾数。求此人是否能经过森林,若能,求出切换护盾的次数。若否,输出 -1 。(备注:进入森林前为未开启状态)

以下是 ac 代码思路:
首先,我们要明白负数 x 与 res 的关系。最麻烦的时候就是每遇到一个负数就要切换两次,则 res = 2*x;其次,是 k 减去负数个数之后的余( k -= x )代表了主人公可以在正数穿上护盾的次数。
首先我们可以找到两负数之间的正数的长度s[],通过 k 的余值对 s 进行填补,每成功填补一次, res 减 2 。最后,对尾部的正数判断一下,若 k 依然可以支撑后面的正数长度,则 res 减 1。

以下是 ac 代码实现:

#include <iostream>#include <algorithm>using namespace std;const int maxn=2e5+4;int s[maxn+5];int main(){    int n,k;    int cnt=0,t,x=0;    cin>>n>>k;    for (int i=0;i<n;i++)//对n长度的串进行输入且处理    {        cin>>t;        if (t>=0) cnt++;//if-else记录了每一段正数的个数        else        {            s[x++]=cnt;//且由s数组记录,数组从0~x            cnt=0;        }    }    if (x>k) return cout<<-1,0;//此处进行特判,若x>k输出-1,结束程序    if (x==0) return cout<<0,0;//若x==0,输出0,结束程序    sort (s+1,s+x);//因为有贪心的思想存在,所以对正数长度进行排序    //另:此处有比较恶心的一点,排序是从1到x排的,而数组是从0~x,换句话说s[0]被忽略了    //至于被忽略的原因:若前方全是正数,还是要从普通状态切换到护盾,若无数字,效果一样    k-=x;    int res=2*x;    for (int i=1;i<x;i++)    {        if (k>=s[i])//此处k用于两负数之间的非负数以减少shift的次数        {            k-=s[i];            res-=2;        }    }    if (k>=cnt) res--;//对尾部正数进行判断    cout<<res<<endl;}

PS:
这次贴的代码有点节俭,且写法比之前的略显装逼,主要是在 CF 上看到了大牛们的极简的代码,我都看我自己不下去,所以也仿着写了一下。所以在代码可读性降低的同时,我会尽量详细地写备注,尽量把代码讲清楚。

第七城市

相关文章

    无相关信息

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台