Poj 3185(开关问题)
来源:互联网 发布:退休工龄算法 编辑:程序博客网 时间:2024/05/17 03:07
The Water Bowls
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 3790 Accepted: 1483
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
USACO 2006 January Bronze
一道简单的开关问题,因为是一个序列,所以第一次翻转的位置一定是最左端右面的那个,或者是最右端左面的那个。就是从左和从右两边模拟,答案取较小值。
#include<cstdio>#include<iostream>#include<algorithm>#include<vector>#include<cstring>#include<queue>#include<stack>using namespace std;const int maxn = 20 + 5;const int INF = 2000000000;typedef pair<int, int> P;typedef long long LL;int a[maxn];int tem[maxn];int main(){ while(scanf("%d",&a[0]) != EOF){ for(int i = 1;i < 20;i++){ scanf("%d",&a[i]); } for(int i = 0;i < 20;i++) tem[i] = a[i]; int cnt1 = 0; for(int i = 0;i < 20;i++){ if(a[i] == 1){ if(i == 19){ cnt1 = INF; break; } cnt1++; a[i+1] = !a[i+1]; a[i+2] = !a[i+2]; } } int cnt2 = 0; for(int i = 19;i >= 0;i--){ if(tem[i] == 1){ if(i == 0){ cnt2 = INF; break; } cnt2++; tem[i-1] = !tem[i-1]; tem[i-2] = !tem[i-2]; } } printf("%d\n",min(cnt1,cnt2)); }}
- Poj 3185(开关问题)
- poj 1830 开关问题(高斯消元(开关问题))
- poj 3185 The Water Bowls(高斯消元 开关问题)
- POJ 3185 The Water Bowls(开关问题)
- poj 1830 开关灯问题(高斯消元法)
- Poj 3279 & 1222(开关问题)
- poj 1830 开关问题(高斯消元)
- poj 1830 开关问题(高斯消元)
- POJ 3276(第二篇)开关问题
- poj 1830 开关问题(数学:高斯消元)
- poj 1830 开关问题(线性方程组,高斯消元)
- POJ-1830 开关问题(高斯消元)
- POJ - 3279 Fliptile(开关问题)
- Fliptile POJ 3279(开关问题)
- POJ 1753 状压(开关问题)
- POJ 1830 开关问题(高斯消元)
- POJ 1830 开关问题(高斯消元)
- (POJ 3279) Fliptile 反转(开关问题)
- [leetcode刷题系列]Combination Sum
- (step4.2.3)hdu 1242(Rescue——BFS)
- Ubuntu13.04下源码安装mysql
- 黑马程序员——Java概述
- UVa 10341 Solve It
- Poj 3185(开关问题)
- UVA10340 All in All (字符串匹配+水题)
- 完美卸载SQL Server 2008的方案
- cakephp笔记——find()函数
- MyEclipse6.5安装SVN插件的三种方法
- HDOJ 2191
- 日前在CVBS的输出中遇到如下问题,特记录
- [leetcode刷题系列]Combination Sum II
- 程序员的十个层次