zoukankan      html  css  js  c++  java
  • Codeforces 549D. Hear Features[贪心 英语]

    D. Haar Features
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    The first algorithm for detecting a face on the image working in realtime was developed by Paul Viola and Michael Jones in 2001. A part of the algorithm is a procedure that computes Haar features. As part of this task, we consider a simplified model of this concept.

    Let's consider a rectangular image that is represented with a table of size n × m. The table elements are integers that specify the brightness of each pixel in the image.

    feature also is a rectangular table of size n × m. Each cell of a feature is painted black or white.

    To calculate the value of the given feature at the given image, you must perform the following steps. First the table of the feature is put over the table of the image (without rotations or reflections), thus each pixel is entirely covered with either black or white cell. The value of a feature in the image is the value of W - B, where W is the total brightness of the pixels in the image, covered with white feature cells, and B is the total brightness of the pixels covered with black feature cells.

    Some examples of the most popular Haar features are given below. 

    Your task is to determine the number of operations that are required to calculate the feature by using the so-called prefix rectangles.

    prefix rectangle is any rectangle on the image, the upper left corner of which coincides with the upper left corner of the image.

    You have a variable value, whose value is initially zero. In one operation you can count the sum of pixel values ​​at any prefix rectangle, multiply it by any integer and add to variable value.

    You are given a feature. It is necessary to calculate the minimum number of operations required to calculate the values of this attribute at an arbitrary image. For a better understanding of the statement, read the explanation of the first sample.

    Input

    The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 100) — the number of rows and columns in the feature.

    Next n lines contain the description of the feature. Each line consists of m characters, the j-th character of the i-th line equals to "W", if this element of the feature is white and "B" if it is black.

    Output

    Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.

    Examples
    input
    6 8
    BBBBBBBB
    BBBBBBBB
    BBBBBBBB
    WWWWWWWW
    WWWWWWWW
    WWWWWWWW
    output
    2
    input
    3 3
    WBW
    BWW
    WWW
    output
    4
    input
    3 6
    WWBBWW
    WWBBWW
    WWBBWW
    output
    3
    input
    4 4
    BBBB
    BBBB
    BBBB
    BBBW
    output
    4
    Note

    The first sample corresponds to feature B, the one shown in the picture. The value of this feature in an image of size 6 × 8 equals to the difference of the total brightness of the pixels in the lower and upper half of the image. To calculate its value, perform the following two operations:

    1. add the sum of pixels in the prefix rectangle with the lower right corner in the 6-th row and 8-th column with coefficient 1 to the variable value (the rectangle is indicated by a red frame); 
    2. add the number of pixels in the prefix rectangle with the lower right corner in the 3-rd row and 8-th column with coefficient  - 2 and variable value

    Thus, all the pixels in the lower three rows of the image will be included with factor 1, and all pixels in the upper three rows of the image will be included with factor 1 - 2 =  - 1, as required.


    感觉英语被虐了,搜了一下题意

    就是求最少的前缀加操作次数,使所有w为1,b为-1


    从右下角开始,按一个之前不会被之后操作前缀包含的顺序,使每一个格子符合要求

    有点像特殊密码锁啊

    //
    //  main.cpp
    //  cf549d
    //
    //  Created by Candy on 9/16/16.
    //  Copyright © 2016 Candy. All rights reserved.
    //
    
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    const int N=105;
    int n,m,a[N][N],s[N][N],ans=0;
    char ts[N];
    inline void fil(int r,int c,int d){
        for(int i=1;i<=r;i++)
            for(int j=1;j<=c;j++) s[i][j]+=d;
    }
    int main(int argc, const char * argv[]) {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++){
            scanf("%s",ts);
            for(int j=0;j<m;j++) a[i][j+1]=(ts[j]=='W'?1:-1);
        }
        for(int i=n;i>=1;i--)
            for(int j=m;j>=1;j--)
                if(s[i][j]!=a[i][j])
                    fil(i,j,a[i][j]-s[i][j]),ans++;
        printf("%d",ans);
        return 0;
    }
  • 相关阅读:
    Atitit 集团与个人的完整入口列表 attilax的完整入口 1. 集团与个人的完整入口列表 1 2. 流量入口概念 2 3. 流量入口的历史与发展 2 1.集团与个人的完整入口列表
    atitit 每季度日程表 每季度流程 v3 qaf.docx Ver history V2 add diary cyar data 3 cate V3 fix detail 3cate ,
    Atitit react 详细使用总结 绑定列表显示 attilax总结 1. 前言 1 1.1. 资料数量在百度内的数量对比 1 1.2. 版本16 v15.6.1 1 1.3. 引入js 2
    Atitit r2017 r3 doc list on home ntpc.docx
    Atitit r2017 ra doc list on home ntpc.docx
    Atiitt attilax掌握的前后技术放在简历里面.docx
    Atitit q2016 qa doc list on home ntpc.docx
    Atitit r7 doc list on home ntpc.docx 驱动器 D 中的卷是 p2soft 卷的序列号是 9AD0D3C8 D:\ati\r2017 v3 r01\
    Atitit 可移植性之道attilax著
    Atitit q2016 q5 doc list on home ntpc.docx
  • 原文地址:https://www.cnblogs.com/candy99/p/5877605.html
Copyright © 2011-2022 走看看