CF549D.Haar Features

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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×mn×m . The table elements are integers that specify the brightness of each pixel in the image.

A feature also is a rectangular table of size n×mn×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 WBW-B , where WW is the total brightness of the pixels in the image, covered with white feature cells, and BB 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.

A 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 valuevalue , 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 valuevalue .

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.

输入格式

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

Next nn lines contain the description of the feature. Each line consists of mm characters, the jj -th character of the ii -th line equals to "W", if this element of the feature is white and "B" if it is black.

输出格式

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

输入输出样例

  • 输入#1

    6 8
    BBBBBBBB
    BBBBBBBB
    BBBBBBBB
    WWWWWWWW
    WWWWWWWW
    WWWWWWWW
    

    输出#1

    2
    
  • 输入#2

    3 3
    WBW
    BWW
    WWW
    

    输出#2

    4
    
  • 输入#3

    3 6
    WWBBWW
    WWBBWW
    WWBBWW
    

    输出#3

    3
    
  • 输入#4

    4 4
    BBBB
    BBBB
    BBBB
    BBBW
    

    输出#4

    4
    

说明/提示

The first sample corresponds to feature BB , the one shown in the picture. The value of this feature in an image of size 6×86×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 66 -th row and 88 -th column with coefficient 11 to the variable valuevalue (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 33 -rd row and 88 -th column with coefficient 2-2 and variable valuevalue .

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

首页