博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3185 The Water Bowls
阅读量:5235 次
发布时间:2019-06-14

本文共 2242 字,大约阅读时间需要 7 分钟。

                                                                                                                                                       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?

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]
 
题意:一共有20个瓶子,现在想让它们全部正面朝上,即20个数全变为0,在首尾处只能同时翻两个瓶子,其他的位置可以同时翻三个瓶子,问至少要翻转几次,所有的瓶子都正面朝上。
思路:可以在最左边瓶子的左边再添一个虚拟的瓶子,这个虚拟的瓶子可以正面朝上或者反面朝上,这样一来从左往右考虑可以把flip[i]定义成区间[i,i+2]是否进行翻转,即可套用书上模型(《挑战程序设计竞赛》)。
虚拟瓶子取1和取0两种情况都考虑一下,取两者中的最少翻转次数即可。
AC代码:
#define _CRT_SECURE_NO_DEPRECATE#include
#include
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
= 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)));}

 

转载于:https://www.cnblogs.com/ZefengYao/p/6505501.html

你可能感兴趣的文章
Redis集群
查看>>
uwsgi基础——服务状态
查看>>
python引入模块
查看>>
初识dubbo(一)
查看>>
没加载redis类,却可以实例化redis
查看>>
软链接mongo
查看>>
时间戳转换
查看>>
hdu 4044 树形DP 炮台打怪 (好题)
查看>>
HDU3336 - Count the string
查看>>
VS2017提醒找不到MSVCR110D.dll
查看>>
[转][Java]简单标签库简介
查看>>
20145206邹京儒 web安全基础实践
查看>>
【python 3.6】如何将list存入txt后,再读出list
查看>>
百度地图API
查看>>
Android实现静默安装与卸载
查看>>
WPF:警惕TextBox会占用过多内存
查看>>
springboot 连接池wait_timeout超时设置
查看>>
Spring @Conditional注解的使用
查看>>
修改mysql max_allowed_packet 配置
查看>>
C#—总结
查看>>