首页 > 代码库 > poj 3185 The Water Bowls
poj 3185 The Water Bowls
The Water Bowls
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6241 | Accepted: 2453 |
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]
题意:一共有20个瓶子,现在想让它们全部正面朝上,即20个数全变为0,在首尾处只能同时翻两个瓶子,其他的位置可以同时翻三个瓶子,问至少要翻转几次,所有的瓶子都正面朝上。
思路:可以在最左边瓶子的左边再添一个虚拟的瓶子,这个虚拟的瓶子可以正面朝上或者反面朝上,这样一来从左往右考虑可以把flip[i]定义成区间[i,i+2]是否进行翻转,即可套用书上模型(《挑战程序设计竞赛》)。
虚拟瓶子取1和取0两种情况都考虑一下,取两者中的最少翻转次数即可。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<algorithm> using namespace std; const int N_MAX = 20 + 1; int pos[N_MAX]; int flip[N_MAX]; int calc(const int&first) { memset(flip,0,sizeof(flip)); pos[0] = first; int sum = 0,res = 0; for (int i = 0; i<N_MAX-1; i++) {//在末尾可以改变两个瓶子的翻转,故i<N_MAX-1,倒数第二个瓶子想反转还是可以翻转的 if ((pos[i] + sum) & 1) { res++; flip[i]++; } sum += flip[i]; if (i - 2 >= 0) sum -= flip[i - 2]; } return res; } int main() { for (int i = 1; i < N_MAX;i++) { scanf("%d",&pos[i]); } printf("%d\n",min(calc(0),calc(1))); }
poj 3185 The Water Bowls
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。