CF-weekly4 D. Haar Features
https://codeforces.com/gym/253910/problem/D
1 second
256 megabytes
standard input
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.
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.
Print a single number — the minimum number of operations that you need to make to calculate the value of the feature.
6 8
BBBBBBBB
BBBBBBBB
BBBBBBBB
WWWWWWWW
WWWWWWWW
WWWWWWWW
2
3 3
WBW
BWW
WWW
4
3 6
WWBBWW
WWBBWW
WWBBWW
3
4 4
BBBB
BBBB
BBBB
BBBW
4
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:
- 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);

- 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的更多相关文章
- Looksery Cup 2015 D. Haar Features 暴力
D. Haar Features Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/549/prob ...
- 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 ...
- [模拟,英语阅读] Codeforces 549D Haar Features
题目:https://codeforces.com/contest/549/problem/D D. Haar Features time limit per test 1 second memory ...
- Codeforces 549D. Hear Features[贪心 英语]
D. Haar Features time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- 论文笔记之:Speed Up Tracking by Ignoring Features
Speed Up Tracking by Ignoring Features CVPR 2014 Abstract:本文提出一种特征选择的算法,来实现用最"精简"的特征以进行目标跟 ...
- 使用OpenCV训练Haar like+Adaboost分类器的常见问题
<FAQ:OpenCV Haartraining>——使用OpenCV训练Haar like+Adaboost分类器的常见问题 最近使用OpenCV训练Haar like+Adaboost ...
- 【图像处理】计算Haar特征个数
http://blog.csdn.net/xiaowei_cqu/article/details/8216109 Haar特征/矩形特征 Haar特征本身并不复杂,就是用图中黑色矩形所有像素值的和减去 ...
- Looksery Cup 2015 Editorial
下面是题解,做的不好.下一步的目标是rating涨到 1800,没打过几次cf A. Face Detection Author: Monyura One should iterate through ...
- LUXURY15
A - Guess Your Way Out! Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
随机推荐
- LG3092 「USACO2013NOV」No Change 状压DP
问题描述 https://www.luogu.org/problem/P3092 题解 观察到 \(k \le 16\) ,自然想到对 \(k\) 状压. 设 \(opt[i]\) 代表使用硬币状况为 ...
- WPF/C# 快捷键 自动生成方法
原文:WPF/C# 快捷键 自动生成方法 这一篇文章会很短~ 在写依赖属性的会后 propdb 会自动生成依赖属性所有的内容 但是如果我写属性变化通知的时候 希望有一个快捷键能自动生成方法 怎 ...
- Windows10 安装grpc-go 详细步骤
准备依赖 git clone https://github.com/grpc/grpc-go.git $env:GOPATH\src\google.golang.org\grpc git clone ...
- python服务不能在docker容器里运行的问题
在开发过程中,我们将mysql.redis.celery等服务在docker容器里跑,项目在本地运行,便于debug调试 docker-compose -f docker-compose-dev.ym ...
- NOI2019退役记 upd:2019.12.1
(我把原来写的东西全部删掉了) AFO. 我退役了,\(\mbox{yyb}\)退役了. 至少,在接下来的日子里,我得投身到文化课,度过快乐的高三生活了. 这两年的\(OI\)生涯给了我很多,让我学会 ...
- 数据库——SQL SERVER Transact-SQL 程序设计
什么是Transact-SQL? 标准SQL不支持过程化控制, 不能完成复杂的功能.T-SQL是过程化SQL语言, 是SQL的扩展 增加了过程化语句 (变量,赋值,分支,循环...)是数据库服务器 ...
- What is Java virtual machine?
Java Virtual Machine (JVM) is a specification that provides runtime environment in which java bytec ...
- Python超详细的字符串用法大全
字符串拼接 实际场景:把列表中的数据拼接成一个字符串 解决方案:使用 str.join() 方法 >>> li = ['cxk', 'cxk', 'kk', 'caibi'] > ...
- Python工具库分享
漏洞及渗透练习平台: WebGoat漏洞练习平台: https://github.com/WebGoat/WebGoat webgoat-legacy漏洞练习平台: https://github.co ...
- 一文解读ARM架构 (转)
本文主要介绍的是arm架构和x86架构的区别,首先介绍了ARM架构图,其次介绍了x86架构图,最后从性能.扩展能力.操作系统的兼容性.软件开发的方便性及可使用工具的多样性及功耗这五个方面详细的对比了a ...