POJ 3185 The Water Bowls(数列反转问题)

The Water Bowls
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 5347 Accepted: 2091

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]

题意:数列有20个数,非别为0或者1。 每翻一个数,可以让0变为1, 1变为0。 翻转的同时,左右两边也跟着翻转。
为最少几次翻转,能让数列全部变为0。

题解:最先当做反转量为3去做,发现做两端的数也可以反转,且反转量为2。故WA。。。。  其实我们只需要判断第一个数需不需要翻转。后面每个数要根据前面一个数的状态决定要不要翻转。前面一个数为1时,需要翻转,反之,则不需。这样枚举第一个数的两种情况就行了。

代码如下:
#include<cstdio>
#include<cstring>
int flib[25]={0},bowl[25]={0};

int solve()
{
	int i,res=100,cnt;
	flib[1]=0; cnt=0;//不翻第一个 
	for(i=2;i<21;++i)
	{
		flib[i]=flib[i-2]^flib[i-1]^bowl[i-1];
		if(flib[i])
			cnt++;
	}
	if((flib[19]^flib[20]^bowl[20])==0)
		res=cnt;
	flib[1]=1; cnt=1;//翻第一个 
	for(i=2;i<21;++i)
	{
		flib[i]=flib[i-2]^flib[i-1]^bowl[i-1];
		if(flib[i])
			cnt++;
	}
	if(cnt<res&&((flib[19]^flib[20]^bowl[20])==0))
		res=cnt;
	return res;
}

int main()
{
	int i;
	for(i=1;i<=20;++i)
		scanf("%d",&bowl[i]);
	int ans=solve();
	printf("%d\n",ans);
	return 0;
} 




<div style="text-align: left;">
</div>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值