The Water Bowls
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7292 | Accepted: 2887 |
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]
给一行0 1 的数, 翻转一个就会使他以及它左右两边的都变, 求最少多少次可以变成全0。
枚举a[1]的两种情况,找出最小值。
详见代码注释
1 #include<cstdio> 2 #include<string.h> 3 #include<algorithm> 4 #include <iostream> 5 using namespace std; 6 int a[25]; 7 bool f[25]; 8 9 int cal() 10 { 11 int i, res = 0; 12 int s = 0; 13 memset(f, 0, sizeof(f)); 14 for (i = 1; i <= 20; i++) 15 { 16 if ((f[i-1]+f[i]+ a[i]) % 2 == 1) 17 { 18 res++; 19 f[i+1] = 1;//通过改变后一个来调整前一个 20 } 21 } 22 return res; 23 } 24 25 int main() 26 { 27 int i; 28 for (i = 1; i <= 20; i++) 29 { 30 cin >> a[i]; 31 } 32 int s = 0; 33 int res1 = 0; 34 res1= cal();//第一种情况 35 int res; 36 a[1] = 1 - a[1];//改变第一个 37 a[2] = 1 - a[2]; 38 res = cal()+1;//第二种情况,别忘了+1 39 res = min(res1, res); 40 cout << res << endl; 41 return 0; 42 }
高斯消元法
1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <cmath> 7 #include <map> 8 #include <set> 9 #include <string> 10 #include <queue> 11 #include <stack> 12 #include <bitset> 13 using namespace std; 14 #define pb(x) push_back(x) 15 #define ll long long 16 #define mk(x, y) make_pair(x, y) 17 #define lson l, m, rt<<1 18 #define mem(a) memset(a, 0, sizeof(a)) 19 #define rson m+1, r, rt<<1|1 20 #define mem1(a) memset(a, -1, sizeof(a)) 21 #define mem2(a) memset(a, 0x3f, sizeof(a)) 22 #define rep(i, n, a) for(int i = a; i<n; i++) 23 #define fi first 24 #define se second 25 typedef pair<int, int> pll; 26 const double PI = acos(-1.0); 27 const double eps = 1e-8; 28 const int mod = 1e9+7; 29 const int inf = 1061109567; 30 const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; 31 int a[50][50], ans[50], x[50]; 32 int n, l[50], free_x[50]; 33 int gauss(int equ,int var) 34 { 35 int i,j,k, max_r, col = 0, temp, free_index, num = 0; 36 for(int i=0;i<=var;i++) 37 { 38 x[i]=0; 39 free_x[i]=0; 40 } 41 for(k = 0;k < equ && col < var;k++,col++) 42 { 43 max_r=k; 44 for(i=k+1;i<equ;i++) 45 { 46 if(abs(a[i][col])>abs(a[max_r][col])) max_r=i; 47 } 48 if(max_r!=k) 49 { 50 for(j=k;j<var+1;j++) swap(a[k][j],a[max_r][j]); 51 } 52 if(a[k][col]==0) 53 { 54 k--; 55 free_x[num++]=col; 56 continue; 57 } 58 for(i=k+1;i<equ;i++) 59 { 60 if(a[i][col]!=0) 61 { 62 for(j=col;j<var+1;j++) 63 { 64 a[i][j] ^= a[k][j]; 65 } 66 } 67 } 68 } 69 for (i = k; i < equ; i++) 70 { 71 if (a[i][col] != 0) return -1; 72 } 73 int stat = 1<<(var-k); 74 int res = inf; 75 for(i=0;i<stat;i++) 76 { 77 int cnt=0; 78 int index=i; 79 for(j=0;j<var-k;j++) 80 { 81 x[free_x[j]]=(index&1); 82 if(x[free_x[j]]) cnt++; 83 index>>=1; 84 } 85 for(j=k-1;j>=0;j--) 86 { 87 int tmp=a[j][var]; 88 int t=0; 89 while(a[j][t]==0)t++; 90 for(int l=t+1;l<var;l++) 91 if(a[j][l]) tmp^=x[l]; 92 x[t]=tmp; 93 if(x[t])cnt++; 94 } 95 if(cnt<res)res=cnt; 96 } 97 return res; 98 } 99 int main() 100 { 101 for(int i = 0; i<20; i++) { 102 scanf("%d", &a[i][20]); 103 } 104 for(int i = 0; i<20; i++) { 105 if(i) { 106 a[i][i-1] = 1; 107 } 108 if(i!=19) 109 a[i][i+1] = 1; 110 a[i][i] = 1; 111 } 112 int ans = gauss(20, 20); 113 cout<<ans<<endl; 114 return 0; 115 }