The Water Bowls
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6489 | Accepted: 2559 |
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?
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]
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
题意:有20个碗依次排列,有的碗口朝上,有的碗口朝下,希望所有的碗口都能朝上,每次翻动某个碗时,左边和右边的碗也会翻转到相反的状态,问要想将这些碗碗口都朝上,至少要翻动多少次
解题思路:每个碗最多翻动一次,否则重复,而且碗先翻与否不影响结果,所以可以贪心法从头到尾考虑每一个碗,对于每个碗,如果左边的碗朝上,则它不能翻动,反之则需要翻动,对第一个碗分翻与不翻两种情况考虑,判断最后一个碗是否朝上且取较小的值即可
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
using namespace std;
#define LL long long
const int INF=0x3f3f3f3f;
int a[30],b[30];
int main()
{
while(~scanf("%d",&a[1]))
{
for(int i=2; i<=20; i++) scanf("%d",&a[i]),b[i]=a[i];
int mi=INF,sum=1;
a[1]^=1,a[2]^=1;
for(int i=2; i<=20; i++)
if(a[i-1]) a[i-1]^=1,a[i]^=1,a[i+1]^=1,sum++;
if(!a[20]) mi=min(mi,sum);
sum=0;
for(int i=2; i<=20; i++)
if(b[i-1]) b[i-1]^=1,b[i]^=1,b[i+1]^=1,sum++;
if(!b[20]) mi=min(mi,sum);
printf("%d\n",mi);
}
return 0;
}