CodeForces - 747D Winter Is Coming(xjb乱搞)

题目链接:http://codeforces.com/problemset/problem/747/D点击打开链接

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的区间上进行操作 有点区间涂色的感觉。。

然后就问m次将夏天变成冬天后 区间上的颜色段数最少

然后开始xjb乱搞。。

记录夏天的所有区段

然后从区段最小的开始变成冬天

然后就wa了。。

这个错误的例子有很多 比如

7 5

-1 0 0 0 -1 0 0

很明显 我们应当去连续的3个0而不是取后面的两个0

因为最后是结束状态

处理方法是把他拿出来最后判断

然后又wa

再来个例子。。

7 5 

0 0 0 -1 0 0 -1

这个时候 开始状态影响了贪心判断

因此得从出现了第一个冬天之后开始判断

然后

还是wa

开始结束状态在同一个区段内

1 1

1

这个时候稍微改下代码判断条件先后顺序即可

不建议看代码 因为很乱很糟糕

如果能体会这些特殊情况就很不错了

#include <bits/stdc++.h>
using namespace std;
struct xjy
{
	int l;
	int r;
};
int cmp1(xjy a,xjy b)
{
	if((a.r-a.l)==(b.r-b.l))
		return a.l<b.l;
	return a.r-a.l<b.r-b.l;
}
vector<xjy> re;
vector<int> s;
int main()
{
	int n,m;

	cin >> n >> m;
	for(int i=0;i<n;i++)
	{
		int mid;
		cin >> mid;
		s.push_back(mid);
	}
	int l=0,r=0,flag=0,summer=0,summerflag=0;
	for(int i=0;i<n;i++)
	{	
		if(s[i]>=0)
		{
			if(!flag)
			{
				l=i;
				flag=1;
			}
			summer++;
		}
		else 
		{
			if(flag)
			{	
				flag=0;
				r=i-1;
				xjy mid;
				mid.l=l;
				mid.r=r;
				if(summerflag)
					re.push_back(mid);
			}
		}
		if(s[i]<0)
			summerflag=1;
	}
	xjy last;
	last.l=1;
	last.r=0;
	if(flag&&summerflag)
	{	
		flag=0;
		r=n-1;
		last.l=l;
		last.r=r;
	}
	//for(int i=0;i<re.size();i++)
	{
		//cout << re[i].l << " " << re[i].r << endl;
	}
	sort(re.begin(),re.end(),cmp1);
	m-=(n-summer);
	flag=0;
	int cnt=0;
	if(m>=0)
		flag=1;
	while(m>=0)
	{
		if(cnt<re.size())
		{	
			if(m>=(re[cnt].r-re[cnt].l+1))
			{
				m-=(re[cnt].r-re[cnt].l+1);
				for(int j=re[cnt].l;j<=re[cnt].r;j++)
					s[j]=-1;
				cnt++;
			}
			else 
				break;
		}
		else 
			break;
	}
	//cout << m << endl;
	if(m>=(last.r-last.l+1))
	{
		for(int j=last.l;j<=last.r;j++)
			s[j]=-1;
	}
	//cout << last.l << " " << last.r << endl; 
	//for(int i=0;i<n;i++)
		//cout << s[i] << " " ;
	//cout << endl;
	int ans=0;
	if(!flag)
	{
		cout <<  "-1" << endl;
	}
	else
	{
		flag=0;
		for(int i=0;i<n;i++)
		{
			if(flag==0&&s[i]<0)
			{
				flag=1;
				ans++;
			}
			else if(flag==1&&s[i]>=0)
			{
				flag=0;
				ans++;
			}
		}
		cout << ans << endl;
	}

	
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值