POJ3185_The Water Bowls_开关问题-3

The Water Bowls
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 6477 Accepted: 2555

Description

The cows have a line of 20 water bowls from which they drink. The bowls can be either right-side-up (properly oriented to serve refreshing cool water) or upside-down (a position which holds no water). They want all 20 water bowls to be right-side-up and thus use their wide snouts to flip bowls. 

Their snouts, though, are so wide that they flip not only one bowl but also the bowls on either side of that bowl (a total of three or -- in the case of either end bowl -- two bowls). 

Given the initial state of the bowls (1=undrinkable, 0=drinkable -- it even looks like a bowl), what is the minimum number of bowl flips necessary to turn all the bowls right-side-up?

Input

Line 1: A single line with 20 space-separated integers

Output

Line 1: The minimum number of bowl flips necessary to flip all the bowls right-side-up (i.e., to 0). For the inputs given, it will always be possible to find some combination of flips that will manipulate the bowls to 20 0's.

Sample Input

0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0

Sample Output

3

Hint

Explanation of the sample: 

Flip bowls 4, 9, and 11 to make them all drinkable: 
0 0 1 1 1 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [initial state] 
0 0 0 0 0 0 0 1 1 0 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 4] 
0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 [after flipping bowl 9] 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 [after flipping bowl 11]

Source

有一列水壶,初始时有的朝上有的朝下。每次可以任选一个水壶反转,但同时必须把它两端的水壶也反转,反转两端的水壶只需连带一个。求最少反转次数使所有水壶都抄上。


乍一看还以为和开关问题 -1 中 k=3 的情况完全一样。wa了一发看了discuss才知道不是。。。

-1 中是必须反转 3 个,此时两端的壶只能被 1 个区间包含。而 -3 则不是。它的两端可以作为反转中心,被两个区间包含。这种情况又和 -2 有点类似,即第一个壶被较小的 n 个区间包含。这种情况下可以先对第一个壶进行一个枚举,后面的就可以递推解决了。


#include<cstdio>
#include<iostream>

using namespace std;

int init[20];
int pro [20];

int Solve ()
{
	int sum = pro[0], mov = 0;

	for(int i= 1; i< 20; i++)
	{
		if(init[i-1] + sum & 1) pro[i] = 1, mov ++;
		else pro[i] = 0;

		sum += pro[i];
		if(i-2 >= 0) sum -= pro[i-2];
	}

	if(init[19] + sum & 1) return -1;
	else return mov;
}

int main ()
{
	for(int i= 0; i< 20; i++)
		cin >> init[i];

	int res = 0x3f;

	//枚举第一个瓶子是否需要反转
	for(int i= 0; i<= 1; i++)
	{
		pro[0] = i;
		int mov = Solve() + i;

		if(mov >= 0 && mov < res) res = mov;
	}


	cout << res << endl;

	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值