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.

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

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 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=;
int n,m,a[N][N],s[N][N],ans=;
char ts[N];
inline void fil(int r,int c,int d){
for(int i=;i<=r;i++)
for(int j=;j<=c;j++) s[i][j]+=d;
}
int main(int argc, const char * argv[]) {
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%s",ts);
for(int j=;j<m;j++) a[i][j+]=(ts[j]=='W'?:-);
}
for(int i=n;i>=;i--)
for(int j=m;j>=;j--)
if(s[i][j]!=a[i][j])
fil(i,j,a[i][j]-s[i][j]),ans++;
printf("%d",ans);
return ;
}

Codeforces 549D. Hear Features[贪心 英语]的更多相关文章

  1. [模拟,英语阅读] Codeforces 549D Haar Features

    题目:https://codeforces.com/contest/549/problem/D D. Haar Features time limit per test 1 second memory ...

  2. codeforces 704B - Ant Man 贪心

    codeforces 704B - Ant Man 贪心 题意:n个点,每个点有5个值,每次从一个点跳到另一个点,向左跳:abs(b.x-a.x)+a.ll+b.rr 向右跳:abs(b.x-a.x) ...

  3. CodeForces - 50A Domino piling (贪心+递归)

    CodeForces - 50A Domino piling (贪心+递归) 题意分析 奇数*偶数=偶数,如果两个都为奇数,最小的奇数-1递归求解,知道两个数都为1,返回0. 代码 #include ...

  4. Codeforces 161 B. Discounts (贪心)

    题目链接:http://codeforces.com/contest/161/problem/B 题意: 有n个商品和k辆购物车,给出每个商品的价钱c和类别t(1表示凳子,2表示铅笔),如果一辆购物车 ...

  5. CodeForces 176A Trading Business 贪心

    Trading Business 题目连接: http://codeforces.com/problemset/problem/176/A Description To get money for a ...

  6. Codeforces Gym 100803C Shopping 贪心

    Shopping 题目连接: http://codeforces.com/gym/100803/attachments Description Your friend will enjoy shopp ...

  7. Codeforces 486C Palindrome Transformation(贪心)

    题目链接:Codeforces 486C Palindrome Transformation 题目大意:给定一个字符串,长度N.指针位置P,问说最少花多少步将字符串变成回文串. 解题思路:事实上仅仅要 ...

  8. Codeforces 1154D - Walking Robot - [贪心]

    题目链接:https://codeforces.com/contest/1154/problem/D 题解: 贪心思路,没有太阳的时候,优先用可充电电池走,万不得已才用普通电池走.有太阳的时候,如果可 ...

  9. codeforces 735C Tennis Championship(贪心+递推)

    Tennis Championship 题目链接:http://codeforces.com/problemset/problem/735/C ——每天在线,欢迎留言谈论. 题目大意: 给你一个 n ...

随机推荐

  1. FOR ALL ENTRIES IN 与 INNER JOIN 写在一个SQL上影响效率

    SELECT likp~vbeln likp~lfart lips~werks likp~kunnr INTO CORRESPONDING FIELDS OF TABLE it_likps FROM ...

  2. 频率直方图(hist)

    频率直方图(frequency histogram)亦称频率分布直方图.统计学中表示频率分布的图形.在直角坐标系中,用横轴表示随机变量的取值,横轴上的每个小区间对应一个组的组距,作为小矩形的底边:纵轴 ...

  3. [转]MOSS通过此命令注册模板,web应用程序可以根据stp模块生成网站集

    注:C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\bin          stsadm –o add ...

  4. iOS 单例模式 浅叙

    单例模式作用 可以保证在程序运行过程中,一个类只有一个实例,而且该实例易于供外界使用 从而方便地控制了实例个数,并节约系统资源 单例模式使用场合 在整个引用程序中,共享一份资源(这份资源只需要创建初始 ...

  5. 如何优化TableView

    关于UITable的优化: 1.最常用的就是不重复生成单元格,很常见,很实用: 2.使用不透明的视图可以提高渲染速度,xCode中默认TableCell的背景就是不透明的: 3.如果有必要减少视图中的 ...

  6. 【代码笔记】iOS-使图片两边不拉伸,中间拉伸

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // ...

  7. C语言中的运算符

    1. 在C语言中运算符包括:算术运算符.关系运算符.赋值运算符.逻辑运算符 2.用运算符把变量.常量连接起来的式子就是表达式 3.我们阅读一个表达式,从表达式的功能和表达式的值来看 4. 算术运算符和 ...

  8. 坑!坑!坑!防不胜防的unsigned int的运算

    我很早之前就知道,unsigned int与int运算的时候,int会被转化为unsigned int来进行运算.一直觉得定这条规则的人是极度反人类的,虽说unsigned int可以表示更大的正值, ...

  9. Unbuntu_14.04编译openjdk7

    今天有问题需要研究一下JVM,但系统挂了,只能重装.在ubuntu下再次编译JDK,大约2个半小时,将遇到的问题笔记整理一下. 1.下载Openjdk Source Code 我用的是http://d ...

  10. junit 使用

    今天用jsoup做了一个‘网络抓取实例’,然而,当作者把junit-4.11.jar 导入项目中,在类中方法上加入@Test,运行时却报错,报错代码如下: java.lang.NoClassDefFo ...