codeforces 747 D. Winter Is Coming (贪心)

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 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的区间,第一种车轮数量任意但是只能覆盖数字大于等于0的地方,另一种车轮数量为k,可以覆盖任意的区域。求更换车轮的最少次数。

这道题有一个很明显的贪心,那就是尽可能将两个负数区间之前的区域也用数量受限的车轮覆盖,那么我们可以将这些区间的长度排序,然后能覆盖就覆盖。

但是直接这么做是不对的。因为中间的区域被覆盖会使次数减少2,但是最头上的区间如果被覆盖成与第一个负数相同的,因为最开始是数量任意的轮子,所以这依然算一次更换,所以对于最头上的区间来说没有影响,直接不考虑即可。而对于最后一个负数位置之后的区域对答案的影响只能减少1,所以我单独考虑最后一个区间,进行分类讨论。中间的按照贪心直接做即可。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 200003
using namespace std;
int n,m,a[N],cnt,mark[N],mark1[N];
struct data
{
	int l,r,len;
}c[N];
int cmp(data a,data b)
{
	return a.len<b.len;
}
int main()
{
	//freopen("a.in","r",stdin);
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]);
	int pos=1;
	while (a[pos]>=0&&pos<=n) pos++;
	m--; mark[pos]=1;
	if (pos>n) {
		printf("0\n");
		return 0;
	}
	for (int i=1;i<=pos-1;i++) mark[i]=1;
	for (int i=pos+1;i<=n;i++)
	 if (a[i]<0) {
	 	mark[i]=1;
	 	c[++cnt].l=pos+1; c[cnt].r=i-1;
	 	c[cnt].len=c[cnt].r-c[cnt].l+1;
	 	m--; pos=i;
	 }
	if (m<0) {
		printf("-1\n");
		return 0;
	}
	//c[++cnt].l=pos+1; c[cnt].r=n;
	//c[cnt].len=c[cnt].r-c[cnt].l+1;
	if (cnt) sort(c+1,c+cnt+1,cmp);
	int t=n-pos; //cout<<pos<<" "<<n<<endl;
	int m1=m-t; int ans=0,ans1=0;
	if (m1>=0) {
		for (int i=1;i<=n;i++) mark1[i]=mark[i];
		for (int i=pos+1;i<=n;i++) mark1[i]=1;
		for (int i=1;i<=cnt;i++) {
		if (c[i].len<=0) continue;
		if (c[i].len>m1) break;
		for (int j=c[i].l;j<=c[i].r;j++) mark1[j]=1,m1--;
	    }
	    if (mark1[1]==1) ans1++;
	    for (int i=2;i<=n;i++)
	    if (mark1[i]!=mark1[i-1]) ans1++;
	}
	else ans1=1000000000;
	for (int i=1;i<=cnt;i++) {
		if (c[i].len<=0) continue;
		if (c[i].len>m) break;
		for (int j=c[i].l;j<=c[i].r;j++) mark[j]=1,m--;
	}
	if (mark[1]==1) ans++;
	for (int i=2;i<=n;i++)
	 if (mark[i]!=mark[i-1]) ans++;
	printf("%d\n",min(ans1,ans));
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值