• 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)));
    }
  • 相关阅读:
    linux 重定向输出到文件、nohup、&
    linux 挂载 iso 镜像文件、CentOS设置本地yum源
    模拟信号、数字信号

    Spring集成数据访问框架、集成NoSql数据库
    spring profile机制测试
    Dockerfile、push到阿里云
    镜像、容器、部署tomcat、修改容器为新镜像、容器卷
    SpringMVC源码
    JNDI
  • 原文地址:https://www.cnblogs.com/ZefengYao/p/6505501.html
Copyright © 2020-2023  润新知