zoukankan      html  css  js  c++  java
  • 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?

    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<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
    ",min(calc(0),calc(1)));
    }
  • 相关阅读:
    cad是什么意思?教你快速把cad转换成pdf格式
    为什么街上的商贩更喜欢用微信支付,而不是支付宝,看完长知识了
    音乐剪辑软件怎么用?教你一个快速编辑音频的方法
    电脑如何录制视频?安利两种电脑录屏的方法
    被称为逆天改命的5大中国工程,曾轰动世界,你知道几个?
    如何使用音乐格式转换器?快速编辑音频文件的方法
    PPT结尾只会说“谢谢”?学会这些PPT结尾,观众主动为你鼓掌
    经典PHP面试题(冒泡排序),当场就被打脸,卧槽什么冒泡?为啥还排序?
    千万不要再搞混了,函数empty( var );输出的判断值是false : true
    PHP删除数组中空数组
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/6505501.html
Copyright © 2011-2022 走看看