Topic:
The water bowls
Time Limit: 1000MS |
|
Memory Limit: 65536K |
Total Submissions: 5013 |
|
Accepted: 1960 |
Description
The cows has a line of water bowls from which they drink. The bowls can either right-side-up (properly oriented to serve refreshing cool water) or upside-down (a position which Holds no water). They want all water bowls to being right-side-up and thus use their wide snouts to flip bowls.
Their snouts, though, is so wide, then they flip not only one bowl but also the bowls on either side of this bowl (a total of three or--in the case of either end bowl--double bowls).
Given the initial state of the bowls (1=undrinkable, 0=drinkable – it even looks like a bowl), what's the minimum number of bowl flips necessary to turn all the bowls right-side-up?
Input
Line 1: 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'll always is possible to find some combination of flips that'll 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 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
Usaco 2006 January Bronze
Test instructions: There are 20 bowls, flipping a bowl will be accompanied by flipping the bowl next to him, asking at least a few times to turn the bowl all the way to the front.
Idea: This problem and POJ1753 very much like, first use the Gaussian elimination method to find the free variable, and then the enumeration to find the minimum value.
Code:
#include <cstdlib> #include <cctype> #include <cstring> #include <cstdio> #include <cmath > #include <climits> #include <algorithm> #include <vector> #include <string> #include < iostream> #include <sstream> #include <map> #include <set> #include <queue> #include < stack> #include <fstream> #include <numeric> #include <iomanip> #include <bitset> #include <list> #include <stdexcept> #include <functional> #include <utility> #include <ctime> Using namespace std, #define PB push_back#define MP make_pair#define REP (i,x,n) for (int i=x;i< (n); ++i) #define for (I,l, h) for (int i= (l); i<= (h), ++i) #define FORD (i,h,l) for (int i= (h); i>= (l); i) #define SZ (x) ((int) (x). Size ()) #define All (x) (x). Begin (), (x). End () #define RI (x) scanf ("%d", & (x)) #define RII (x, Y) scanf ("%d%d", & (X), & (Y)) # Define RIII (x, Y, z) scanf ("%d%d%d", & (X), & (Y), & (z)) #define DRI (x) int (x); scanf ("%d", &x) #define DRII (x, y) int X, y; scanf ("%d%d", &x, &y) #define DRIII (x, y, z) int x, y, Z; scanf ("%d%d%d", &x, &y, &z) #define OI (x) printf ("%d", x), #define RS (x) scanf ("%s", (x)) #define MS0 (x) memset ( (x), 0, sizeof ((x))) #define MS1 (x) memset ((x),-1, sizeof ((x))) #define LEN (x) strlen (x) #define F first#define S second#def Ine Swap (A, B) (a ^= B, b ^= A, a ^= b) #define Dpoint strcut node{int x, y} #define CMPD int cmp (const int &A,CONST int &B) {return a>b;}/* #ifdef HOME freopen ("In.txt", "R", stdin); #endif */const int MOD = 1e9+7;typedef vector<int> vi;typedef vector<string> vs;typedef vector<double> Vd;typedef Long Long ll;typedef pair<int,int> pii;//#define Homeint Scan () {int res = 0, ch, flag = 0;if (ch = getcha R ()) = = '-')//determine positive and negative flag = 1;else if (ch >= ' 0 ' && ch <= ' 9 ')//Get complete number res = CH-' 0 '; while ((ch = getchar ()) > = ' 0 ' && ch <= ' 9 ') res = res * + CH-' 0 '; return flag? -res:res;}/*---------------------Do----------HACK-----ME--------------------*/const int maxn=50;int A[MAXN][MAXN] ;//augmented matrix int x[maxn];//solution int free_x[maxn];//tag is an indeterminate variable/*void Debug (void) {int I, J; for (i = 0, i < equ; i++) {for (j = 0, J < var + 1; j + +) {cout << a[i][j] <&l T " "; } cout << Endl; } cout << Endl;} */inline int gcd (int a,int b) {int t; while (b!=0) {t=b; B=a%b; a=t; } return A; The inline int LCM (int a,int b) {return A/GCD (A, a) *b;//first divide and then multiply the anti-overflow}//Gaussian elimination method to solve the equation set (Gauss-jordan elimination). (-2 means that there is a floating-point solution, but no integer solution,//-1 represents no solution, 0 means the unique solution, greater than 0 is the infinite solution, and returns the number of free arguments)//There are equ equations, var variable element. The number of augmented matrix rows is equ, 0 to Equ-1, and the number of columns is var+1, respectively, 0 to Var.int Gauss (int equ,int var) {int i,j,k; int max_r;//The row with the largest absolute value for the current column. int col;//the currently processed column int TA,TB; int LCM; int temp; int free_x_num; int free_index; for (int i=0;i<=var;i++) {x[i]=0; free_x[i]=0; } free_x_num=0; Converted to a stepped array. Col=0; The currently processed column for (k = 0;k < equ && Col < var;k++,col++) {//enumerates the currently processed rows.//The row that finds the largest absolute value of the col column element is exchanged with the K line. To reduce the error when dividing) max_r=k; for (i=k+1;i<equ;i++) {if (ABS (A[i][col]) >abs (A[max_r][col])) max_r=i; } if (max_r!=k) {//is exchanged with line K. for (j=k;j<var+1;j++) swap (a[k][j],a[max_r][j]); } if (a[k][col]==0) {//indicates that the COL column K line below is all 0, then the next column of the current row is processed. k--; Free_x[free_x_num++]=col; Continue } for (i=k+1;i<equ;i++) {//enumerates the rows to be deleted. if (a[i][col]!=0) {LCM = LCM (ABS (A[i][col]), ABS (A[k][col])); Ta = Lcm/abs (A[i][col]); TB = Lcm/abs (A[k][col]); if (a[i][col]*a[k][col]<0) tb=-tb;//, the case is added for (j=col;j<var+1;j++) { A[I][J] = A[i][j]^a[k][j]; }}}}//Debug (); 1. The absence of solution: the existence of the augmented array of simplification (0, 0,.).., a) such a line (a! = 0). for (i = k; i < equ; i++) {//For infinite solutions, if you want to determine which are free-variable, then the Exchange in the elementary row transformation will be affected, then the interchange should be recorded. if (A[i][col]! = 0) return-1; }//2. The case of Infinity: in the augmented array of Var * (var + 1) (0, 0, ..., 0) Such a line, that is to say, does not form a strict upper triangular array. and the number of rows that appear is the number of free arguments. First, the free variable has var-k, that is, the indeterminate variable has at least var-k. return var-k; There are var-k of free-variable elements. 3. The only solution: a strict upper triangular array is formed in the augmented array of Var * (var + 1). Calculate the Xn-1, Xn-2 ... X0.} int main () {for (int i=0;i<20;i++) RI (a[i][20]), for (int i=0;i<20;i++) {a[i][i]=1; if (i!=0) a[i-1][i]=1; if (i!=19) a[i+1][i]=1;} int T=gauss (20,20); int tot=1<<t;int ans=int_max;for (int i=0;i<tot;i++) {int cnt=0; MS0 (x); for (int j=0;j<t;j++) {if (i& (1<<j)) {x[free_x[j]]=1; if (X[free_x[j]]) cnt++; }} for (int j=19-t;j>=0;j--) {int temp=a[j][20]; for (int k=j+1;k<20;k++) {if (A[j][k]) temp^=x[k]; } X[j]=temp; if (X[j]) cnt++; } ans=min (ans,cnt);} printf ("%d\n", ans); return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3185 The water bowls (Gaussian elimination method, enumeration of free elements)