POJ_3185_The_Water_Bowls_(反转)

描述


 

http://poj.org/problem?id=3185

20个碗,要全部反转过来.反转某个碗,其相邻的碗(左右两边)也要反转.

 

The Water Bowls
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 5538   Accepted: 2172

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]

Source

分析


反转(开关问题).

这一类问题的一个小变形.唯一的不同之处在于在区间边缘可以用长度为2的区间,这就让问题不能裸着做了= =.由于区间只存在转和不转,并且没有先后顺序,所以对于左边的长度为2的区间,只有转/不转两种情况,右同.所以共有2*2=4中情况,于是我们可以分这4种情况,把每一种情况中两个特殊区间的都先处理完,然后剩下的区间就是模板问题了.再来思考右边的那个长度为2的区间,对于转或不转可以在最后进行判断,故只要讨论2种情况即可.

注意点:

1.对于右边的长度为2的区间,如果要转不能忘了res++.

2.这道题n=20,k=3,做起来方便.可以思考任取的n与k.

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using std :: min;
 5 
 6 const int maxn=25,n=20,k=3,INF=0x7fffffff;
 7 bool b[maxn],f[maxn];
 8 int ans=INF;
 9 
10 void solve(int res)
11 {
12     bool sum=false;
13     memset(f,false,sizeof(f));
14     for(int i=1;i<=n-2;i++)
15     {
16         if(b[i]^sum)
17         {
18             f[i]=true;
19             sum^=true;
20             res++;
21         }
22         if(i-2>0) sum^=f[i-2];
23     }
24     bool l2=b[n-1]^sum;
25     sum^=f[n-3];
26     bool l1=b[n]^sum;
27     if(l2!=l1) return;
28     if(l2==true) res++;
29     ans=min(ans,res);
30 }
31 
32 void init()
33 {
34     for(int i=1;i<=n;i++)
35     {
36         int a;
37         scanf("%d",&a);
38         b[i]=a==1 ? true : false;
39     }
40     solve(0);
41     b[1]=!b[1];
42     b[2]=!b[2];
43     solve(1);
44     printf("%d\n",ans);
45 }
46 
47 int main()
48 {
49     freopen("water.in","r",stdin);
50     freopen("water.out","w",stdout);
51     init();
52     fclose(stdin);
53     fclose(stdout);
54     return 0;
55 }
View Code

 

posted @ 2016-04-22 23:33  晴歌。  阅读(254)  评论(0编辑  收藏  举报