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

Sample test(s)
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 twooperations:

  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。注意更新的时候要从最左上角開始更新。

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<algorithm>
using namespace std;
char s[105][105];
int map[105][106];
int main()
{
int n,m,i,j,c,ans,r,num;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=0;i<n;i++){
scanf("%s",s[i]);
}
num=0;
memset(map,0,sizeof(map));
for(i=n-1;i>=0;i--){
for(j=m-1;j>=0;j--){
if(s[i][j]=='W'){
if(map[i][j]!=1){
num++;
ans=1-map[i][j];
for(r=i;r>=0;r--){
for(c=j;c>=0;c--){
map[r][c]+=ans;
}
}
}
}
else if(s[i][j]=='B'){
if(map[i][j]!=-1){
num++;
ans=map[i][j]+1;
for(r=i;r>=0;r--){
for(c=j;c>=0;c--){
map[r][c]-=ans;
}
}
}
}
}
}
printf("%d\n",num);
}
return 0;
}

codeforces Looksery Cup 2015 D. Haar Features的更多相关文章

  1. Looksery Cup 2015 D. Haar Features 暴力

    D. Haar Features Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/prob ...

  2. codeforces Looksery Cup 2015 H Degenerate Matrix

    The determinant of a matrix 2 × 2 is defined as follows: A matrix is called degenerate if its determ ...

  3. codeforces Looksery Cup 2015 H Degenerate Matrix 二分 注意浮点数陷阱

    #include <cstdio> #include <cstring> #include <algorithm> #include <string> ...

  4. codeforces Looksery Cup 2015 C. The Game Of Parity

    There are n cities in Westeros. The i-th city is inhabited by ai people. Daenerys and Stannis play t ...

  5. Looksery Cup 2015 Editorial

    下面是题解,做的不好.下一步的目标是rating涨到 1800,没打过几次cf A. Face Detection Author: Monyura One should iterate through ...

  6. Looksery Cup 2015 A. Face Detection 水题

    A. Face Detection Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/pro ...

  7. Looksery Cup 2015 B. Looksery Party 暴力

    B. Looksery Party Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/pro ...

  8. Codeforces VK CUP 2015 D. Closest Equals(线段树+扫描线)

    题目链接:http://codeforces.com/contest/522/problem/D 题目大意:  给你一个长度为n的序列,然后有m次查询,每次查询输入一个区间[li,lj],对于每一个查 ...

  9. Looksery Cup 2015 H. Degenerate Matrix 数学

    H. Degenerate Matrix Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/ ...

随机推荐

  1. STMP服务器发送邮件,本地可以发送但是服务器一直发送不成功;

    在官网上查看到信息 考虑到部分云服务商封禁了其内网对外 25 端口的访问, xxxxx 端口号: 2525 xxxxx 端口号: 587 然后,我换了一下端口号就行了,浪费了我三个小时时间,贼尴尬:

  2. Linq学习(二)-本次学习用到的资料

    本次学习用到的数据库初始化脚本如下 use KMS create table Blog_User ( UserId ,1), NickName ), CreateTime datetime ) cre ...

  3. 新认知之WinForm窗体程序

    Windows应用程序和控制台应用程序有很大的区别 >Form1.cs  :窗体文件,程序员对窗体编写的代码一般都存放在这个文件中. >Form1.Designer.cs :窗体设计文件, ...

  4. 关于debug.keystore文件用法以及错误处理

    在开发过程中需要频繁的为测试的同事签名apk,非常很麻烦,把默认debug.keystore文件替换成发布用(生产环境)的签名文件,不用频繁地签名apk文件了.      如果直接使用生产keysto ...

  5. php入门学习相关函数

      1.join(): 定义和用法 join() 函数返回由数组元素组合成的字符串. join() 函数是 implode() 函数的别名. 注释:join() 函数接受两种参数顺序.但是由于历史原因 ...

  6. 使用码云gitee.com托管代码

    1.新建项目 可以看到团队资源管理器是这样的,已经在本地有存储库,所有更改可以保存本地 2.在码云上新建项目 项目名称必填,其它项根据情况填写 3.复制项目地址关联到本地存储库 填写码云的项目地址,发 ...

  7. C# 金钱添加逗号0000

    private void Form1_Load(object sender, EventArgs e) { decimal dd = (decimal)11234567890.01; string d ...

  8. C# SqlParameter 使用

    //System.Data.SqlClient.SqlParameter[] sqlParameters = new System.Data.SqlClient.SqlParameter[]{ };  ...

  9. 关于在win7旗舰版32位上 安装 net4.0 的闪退问题研究 和安装sqlserver2008问题

    1.配置文件客户端[目标x86x64]的 可以安装 2.配置文件完全的目标x86x64的 出现闪退. 3.服务器核心的出现无法安装 安装 sqlserver 2008R2数据库 报错 \最后留下了它, ...

  10. 程序员不可不知的Linux性能工具

    前言 在实际开发中,有时候会收到一些服务的监控报警,比如CPU飙高,内存飙高等,这个时候,我们会登录到服务器上进行排查.本篇博客将涵盖这方面的知识:Linux性能工具. 一次线上问题排查模拟 背景:服 ...