https://codeforces.com/gym/253910/problem/D

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 Bis 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

Copy
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
output

Copy
2
input

Copy
3 3
WBW
BWW
WWW
output

Copy
4
input

Copy
3 6
WWBBWW
WWBBWW
WWBBWW
output

Copy
3
input

Copy
4 4
BBBB
BBBB
BBBB
BBBW
output

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

题目大意:

给出一个n*m的棋盘,每个小方格都是白色或者黑色,要计算白色格子的个数减去黑色格子个数的结果

给出一个操作:每次操作可以选择一个方格,然后计算它的前缀方格的个数乘以一个系数的结果。

问要得到最终答案,至少进行几次操作

题解:

从后往前枚举,即n->1,m->1,每当发现一个白色的格子不是1,或者黑色的格子不是-1,就将它以及它的前缀矩阵加上相应的值,使当前格子的值变成相应的1或者-1

复杂度为O(n^4)

#include<iostream>
#include<cstdio>
#include<cstring>
#define maxn 110
using namespace std;
int n,m,a[maxn][maxn],ans;
char s[maxn][maxn];
int main(){
// freopen("Cola.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
scanf("%s",s[i]+);
for(int i=n;i>=;i--){
for(int j=m;j>=;j--){
if(s[i][j]=='W'&&a[i][j]!=){
int add=-a[i][j];
ans++;
for(int x=i;x>=;x--)
for(int y=j;y>=;y--)
a[x][y]+=add;
}
else if(s[i][j]=='B'&&a[i][j]!=-){
int add=--a[i][j];
ans++;
for(int x=i;x>=;x--)
for(int y=j;y>=;y--)
a[x][y]+=add;
}
}
}
printf("%d\n",ans);
return ;
}

CF-weekly4 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 D. Haar Features

    The first algorithm for detecting a face on the image working in realtime was developed by Paul Viol ...

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

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

  4. Codeforces 549D. Hear Features[贪心 英语]

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

  5. 论文笔记之:Speed Up Tracking by Ignoring Features

    Speed Up Tracking by Ignoring Features CVPR 2014 Abstract:本文提出一种特征选择的算法,来实现用最"精简"的特征以进行目标跟 ...

  6. 使用OpenCV训练Haar like+Adaboost分类器的常见问题

    <FAQ:OpenCV Haartraining>——使用OpenCV训练Haar like+Adaboost分类器的常见问题 最近使用OpenCV训练Haar like+Adaboost ...

  7. 【图像处理】计算Haar特征个数

    http://blog.csdn.net/xiaowei_cqu/article/details/8216109 Haar特征/矩形特征 Haar特征本身并不复杂,就是用图中黑色矩形所有像素值的和减去 ...

  8. Looksery Cup 2015 Editorial

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

  9. LUXURY15

    A - Guess Your Way Out! Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

随机推荐

  1. jenkins下载插件无插件显示+离线下载插件方法

    1.打开Jenkins插件管理,可选插件为空,无法选择自己需要的插件进行下载 打开插件管理的“高级”选项,在升级站点填写 http://mirror.xmission.com/jenkins/upda ...

  2. django实现多种支付、并发订单处理

    django实现多种支付方式 ''' #思路 我们希望,通过插拔的方式来实现多方式登录,比如新增一种支付方式,那么只要在项目中新增一个py文件,导入里面的pay方法就可以了,这样在支付业务中支付语句是 ...

  3. 解决问题:Red Hat Enterprise Linux 7 64 位 虚拟机安装后无法启动图形化

    原因: 1.系统在创建时,没有安装图形化 2.系统在安装后,有降低内存的操作,内存过低无法启动桌面,以及其他 就原因一进行图形化安装: 1.VMware挂载Red Hat Enterprise Lin ...

  4. R-长尾词练习

    一. 长尾关键词的特征 长尾关键词通常比较长,往往是2-3个词组成,甚至是短语,存在于内容页面,除了内容页的标题,还存在于内容中. 长尾关键词搜索量虽然非常少,而且不稳定.但是搜索量甚至超越热门目标关 ...

  5. thinkphp5.1单模块设置

    thinkphp5.1单模块 1. // 是否支持多模块'app_multi_module' => false, // 自动搜索控制器'controller_auto_search' => ...

  6. tushare+pandas实现财经数据分析

    写在前面的话: 这是一个优秀的财经接口包,博主平时工作中也有使用,觉得很好,现在分享一些使用心得给需要的人,tushare并不是一个炒股软件,只是一个提供pandas数据的工具,具体如何使用,因人而异 ...

  7. 21个Java Collections面试问答

    Java Collections框架是Java编程语言的核心API之一. 这是Java面试问题的重要主题之一.在这里,我列出了一些重要的Java集合面试问题和解答,以帮助您进行面试.这直接来自我14年 ...

  8. 【Linux】Linux 性能瓶颈阈值分析

    Linux系统资源包括:CPU.IO(磁盘和网络).内存等 利用率达到三个阶段时: 1)50% 引起注意 2)70% 密切关注 3)90% 严重情况 vmstat.sar.iostat.mpstat. ...

  9. Long类型数据前端精度丢失

    问题描述 后端把Long类型的数据传给前端,前端可能会出现精度丢失的情况.例如:201511200001725439这样一个Long类型的整数,传给前端后会变成201511200001725440 相 ...

  10. 在windows系统上面部署springboot项目并设置其开机启动

    前言 最近的项目需要在客户的服务器上面部署一个项目然后进行测试,服务器的系统是windows server2008的,以前部署的项目都是在linux系统上面居多,就算是在windows系统上面自己玩的 ...