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.

【题目链接】:http://codeforces.com/contest/747/problem/D

【题解】



找到最左边的l,满足t[l]<0,找到最右边的r,满足t[r]<0;

然后在l..r这个区间里面

找连续的t>=0的块

把每个块记录起来;

然后在l..r里面

统计t>=0的个数->a

统计t<0的个数->b

如果b>k则无解;

如果b<=k;

设rest = k-b;

则看看l..r里面哪一些>=0的连续块能够用rest抵消掉;

即让雪地胎在温度大于等于0的时候用.这样这一块就不用换胎了(减少了2次换胎);

显然我们是要让块数最多。

那么每次找块的长度最短的减就好了;

减掉后标记这一块被减掉了;否则

对那些没被减掉的块,答案都递增2;

还有在l之前肯定是夏天胎。则肯定要递增一次;

如果在r后面还有温度大于0的,则要看能不能直接走到最后->即rest还有没有剩余;否则还要递增答案1;

这个可以把r+1..n单独作为一块加到已经升序排完后的>=0块vecotr后面;因为这个对答案的递减作用只有1;而哪些>=0的块对答案的递减作用有2;所以最后再考虑它.

全都是正数的情况特判下.



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int MAXN = 2e5+100;
const int INF = 21e8;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0); struct abc
{
int l,r,len,flag;
}; int n,k,t[MAXN],trick[MAXN];
int cnt = 0;
vector <abc> a; bool cmp(abc a,abc b)
{
return a.len<b.len;
} int main()
{
//freopen("F:\\rush.txt","r",stdin);
rei(n);rei(k);
rep1(i,1,n)
rei(t[i]);
int l = -1,r = -1;
rep1(i,1,n)
if (t[i]<0)
{
l = i;
break;
}
rep2(i,n,1)
if (t[i] <0)
{
r = i;
break;
}
if (l==-1)
{
cout << 0<<endl;
return 0;
}
rep1(i,l,r)
{
if (t[i]>=0)
{
int ll = i,rr = i;
while (rr+1<=r && t[rr+1]>=0) rr++;
a.pb({ll,rr,rr-ll+1,1});
i = rr;
}
}
sort(a.begin(),a.end(),cmp);
if (r<n)
a.pb({r+1,n,n-(r+1)+1,1});
else
a.pb({r+1,n,INF,0});
int aa = 0,bb = 0;
rep1(i,l,r)
if (t[i] >=0)
aa++;
else
bb++;
int now = k-bb;
if (now <0)
{
puts("-1");
return 0;
}
int len = a.size();
rep1(i,0,len-1)
{
if (now >= a[i].len)
{
now-=a[i].len;
a[i].flag = 0;
}
}
int ans = 0;
rep1(i,0,len-2)
if (a[i].flag==1)
ans +=2;
if (a[len-1].flag==1)
ans += 1;
ans+=1;
cout << ans << endl;
return 0;
}

【23.26%】【codeforces 747D】Winter Is Coming的更多相关文章

  1. 【23. 合并K个排序链表】【困难】【优先队列/堆排序】

    合并 k 个排序链表,返回合并后的排序链表.请分析和描述算法的复杂度. 示例: 输入: [ 1->4->5, 1->3->4, 2->6] 输出: 1->1-> ...

  2. 【 BowWow and the Timetable CodeForces - 1204A 】【思维】

    题目链接 可以发现 十进制4 对应 二进制100 十进制16 对应 二进制10000 十进制64 对应 二进制1000000 可以发现每多两个零,4的次幂就增加1. 用string读入题目给定的二进制 ...

  3. Codeforces 747D:Winter Is Coming(贪心)

    http://codeforces.com/problemset/problem/747/D 题意:有n天,k次使用冬天轮胎的机会,无限次使用夏天轮胎的机会,如果t<=0必须使用冬轮,其他随意. ...

  4. 【26.83%】【Codeforces Round #380C】Road to Cinema

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. 【20.23%】【codeforces 740A】Alyona and copybooks

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  6. 【23.33%】【codeforces 557B】Pasha and Tea

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  7. 【23.39%】【codeforces 558C】Amr and Chemistry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  8. 【30.23%】【codeforces 552C】Vanya and Scales

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 【26.09%】【codeforces 579C】A Problem about Polyline

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

随机推荐

  1. 通过IP地址訪问Jbossserver上的应用

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/liu765023051/article/details/28882533 环境介绍 Web项目中.在 ...

  2. 只想着一直调用一直爽, 那API凭证泄漏风险如何破?

    如今各家云厂商都通过给用户提供API调用的方式来实现一些自动化编排方面的需求.为了解决调用API过程中的通信加密和身份认证问题,大多数云厂商会使用同一套技术方案—基于非对称密钥算法的鉴权密钥对,这里的 ...

  3. MaxCompute助力小影短视频走向全球化

    数字时代,中国已经成为世界互联网的中心,小影(海外版称作为VivaVideo,后简称VivaVideo)作为国内首批短视频出海企业,借助统一的云计算平台快速实现全球业务的线上部署,已经让每一行代码都获 ...

  4. 1.27eia原油

  5. python ASCII编码集

  6. uml图的五种关系 标签: uml 2016-12-18 21:47 221人阅读 评论(25) 收藏

    统一建模语言 Unified Modeling Language (UML)又称统一建模语言或标准建模语言,是始于1997年一个OMG标准,它是一个支持模型化和软件系统开发的图形化语言,为软件开发的所 ...

  7. docker安装 2016-11-06 19:14 299人阅读 评论(31) 收藏

    Docker支持运行在以下CentOS版本: CentOS 7.X 安装在二进制兼容的EL7版本如 Scientific Linux也是可能成功的,但是Docker 没有测试过并且不官方支持. 此文带 ...

  8. Effective C++: 02构造、析构、赋值运算

    05:了解C++默默编写并调用哪些函数 1:一个空类,如果你自己没声明,编译器就会为它声明(编译器版本的)一个copy构造函数.一个copy assignment操作符和一个析构函数.此外如果你没有声 ...

  9. MUI - 解决动态列表页图片懒加载再次加载不成功的bug

    首先描述一下功能 实现列表页动态加载 通过官方提供的"下拉刷新和上拉刷新"及"图片懒加载"示例实现. http://www.cnblogs.com/philly ...

  10. HZOJ 连连看

    考场几乎想到了正解,然而我也不知道当时在想啥,在没有证伪的情况下只是觉得无法实现就否了…… 最后打的好象是达哥说的O(4*15*n*m),复杂度不是很会证反正T成了暴力…… 题解: 对于测试点8,9, ...