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?

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

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));    }}


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘客网站被微信屏蔽怎么办 微信老是屏蔽网站怎么办 华帝售后没人理怎么办 新开的网店没生意怎么办 淘小二介入买家举证不全怎么办 新股申购中签后钱不够怎么办 买的东西收不到怎么办? 拼多多按错收货怎么办 团购招生做到一半不如意怎么办 网购付款后卖家没有货怎么办 淘宝退货商家不同意退款怎么办 淘宝确认收货后卖家拒绝退款怎么办 淘宝卖家不同意退款怎么办 淘宝仅退款卖家不处理怎么办 淘宝申请退款卖家不处理怎么办 快递没收到货要退款怎么办 发票给了不给钱怎么办 付款后不给发票怎么办 供货商不给发票怎么办 刚生过孩子太胖买衣服怎么办 黑色牛仔裤洗的发白怎么办 蘑菇街手机丢了怎么办 黑衣服上全是白毛毛怎么办 支付宝注销了钱怎么办 网上买东西手机号写错了怎么办 二类工资卡过万怎么办 淘宝发货地址写错怎么办 淘宝发货地址写错了怎么办 淘宝不能代付了怎么办 支付宝付款码被盗刷怎么办 地方选举追究不到相关责任人怎么办 天正建筑画个直线找不到怎么办 Wi-Fi模块不支持多播怎么办? 魅族手机屏幕点不动怎么办 uc打开网页很慢怎么办 京东手机号码无法登录怎么办 织梦系统网站没收录怎么办 电脑开机出现一堆乱码怎么办 电脑文件夹出现乱码打不开怎么办 电脑出现f1和f2怎么办 电脑中韩文内容显示乱码怎么办