CodeForces

来源:互联网 发布:淘宝可以上闲鱼吗 编辑:程序博客网 时间:2024/05/21 19:36

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·1050 ≤ 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 62 -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天内行驶 车有两种轮胎 一种只能在气温大于等于0行驶 一种任意气温都可以 第二种轮胎有使用的天数限制 第一种没有 开始时默认是第一种轮胎 每天开始时都可以更换轮胎 问最少更换轮胎的次数


思路:初看以为是dp的题 想了半天找不到递推方程 后来看了大佬的博客才知道这竟然是贪心...

输入时数下零下气温的天数 并把每个相邻气温的中间气温的天数都记录下来放在vector里

若总共能使用特殊轮胎的天数小于零下的天数 输出-1

初始时设要换的次数为零下的天数乘2 即每一次零下都早上换成特殊轮胎晚上再换回普通轮胎 然后遍历vector 如果k能覆盖这几天 就把要换的次数减2 相当于减掉了前一个零下晚上和后一个零下早上的换轮胎的次数 最后再做一个特判看特殊轮胎是不是能用到最后


#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <queue>#include <stack>#include <map>#include <cmath>#include <vector>#define max_ 200010#define inf 0x3f3f3f3f#define ll long longusing namespace std;int n,k;int num[max_];vector<int>v;int main(int argc, char const *argv[]){scanf("%d%d",&n,&k);int i,cnt=0,pre=-1;for(i=1;i<=n;i++){scanf("%d",&num[i]);if(num[i]<0){cnt++;if(pre==-1)pre=i;else{v.push_back(i-pre-1);pre=i;}}}if(cnt>k){printf("-1\n");return 0;}sort(v.begin(), v.end());k-=cnt;int ans=2*cnt;for(i=0;i<v.size();i++){if(v[i]<=k){ans-=2;k-=v[i];}elsebreak;}if(k>=n-pre)ans--;printf("%d\n",ans);return 0;}


原创粉丝点击