The Water Bowls
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6731 | Accepted: 2650 |
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
/* * @Author: Lyucheng * @Date: 2017-08-04 15:12:25 * @Last Modified by: Lyucheng * @Last Modified time: 2017-08-04 17:00:37 */ /* 题意:给你一个20的01序列,每次你可以反转相邻的三个点,问你至少反转多少次才能使得所有的点变成0 思路:开关反转问题,一个序列如果有解的话,那么需要反转的区间是一定的,有个区间反转两次是没有 意义的,如果一个点为1那么所在区间是一定要进行反转的,并且只翻转一次,所有可以两头开始模拟 反转,比较一下大小 */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> #include <vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h> #include <stdlib.h> #include <time.h> #define MAXN 25 using namespace std; int pos1[MAXN]; int pos2[MAXN]; int res1; int res2; inline void init(){ res1=0; res2=0; } int main(){ // freopen("in.txt", "r", stdin); // freopen("out.txt", "w", stdout); while(scanf("%d",&pos1[1])!=EOF){ pos2[1]=pos1[1]; for(int i=2;i<=20;i++){ scanf("%d",&pos1[i]); pos2[i]=pos1[i]; } init(); for(int i=1;i<=20;i++){ if(pos1[i]==1){ if(i==20) res1=21;//无解 res1++; pos1[i]=!pos1[i]; pos1[i+1]=!pos1[i+1]; pos1[i+2]=!pos1[i+2]; } } for(int i=20;i>=1;i--){ if(pos2[i]==1){ if(i==1) res2=21;//无解 res2++; pos2[i]=!pos2[i]; pos2[i-1]=!pos2[i-1]; pos2[i-2]=!pos2[i-2]; } } printf("%d ",min(res1,res2)); } return 0; }