codeforces Looksery Cup 2015 D. Haar Features

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;
}
时间: 2024-11-05 23:37:59

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

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

#include <cstdio> #include <cstring> #include <algorithm> #include <string> #include <cmath> #include <iostream> using namespace std; bool fun(double l1,double r1,double l2,double r2){ return (l1 <= r2 && l1

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 determinant is equal to zero. The norm ||A|| of a matrix A is defined as a maximum of absolute values of its elements. You are given a matrix . Consider any

Looksery Cup 2015 Editorial

下面是题解,做的不好.下一步的目标是rating涨到 1800,没打过几次cf A. Face Detection Author: Monyura One should iterate through each 2x2 square and check if it is possible to rearrange letters in such way they they form the word "face". It could be done i.e. by sorting al

Codeforces VK Cup 2015 Wild Card Round 1 (AB)

比赛链接:http://codeforces.com/contest/522 A. Reposts time limit per test:1 second memory limit per test:256 megabytes One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started

Looksery Cup 2015——AFace Detection

The developers of Looksery have to write an efficient algorithm that detects faces on a picture. Unfortunately, they are currently busy preparing a contest for you, so you will have to do it for them. In this problem an image is a rectangular table t

Codeforces VK Cup 2015 A.And Yet Another Bracket Sequence(后缀数组+平衡树+字符串)

这题做得比较复杂..应该有更好的做法 题目大意: 有一个括号序列,可以对其进行两种操作: ·        向里面加一个括号,可以在开头,在结尾,在两个括号之间加. ·        对当前括号序列进行循环移动,即把最后一个括号拿到开头来. 上述两种操作可以做任意次,要求添加最少的括号使得原序列变成一个合法括号序列.如果有多种可能,输出字典序最小的那一个."(" < ")". 题解: 首先计算左括号和右括号的数量,可以知道,不妨假设左括号的数量大于右括号 那么

codeforces #549 Looksery Cup 部分题解

掉Rating快乐~~ A.Face Detection 题目大意:给定一个n?m的矩阵,求有多少2?2的子矩形满足单词"face"的每个字母在矩形中恰好出现一次 签到题 #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 55 using namespace std; int n,m,ans; char map[M

Codeforces gym Hello 2015 Div1 B and Div2 D

Codeforces gym 100571 problem D Problem 给一个有向图G<V,E>和源点S,边的属性有长度L和颜色C,即E=<L,C>.进行Q次询问,每次给定一个点X,输出S到X的最短路的长度(不存在则输出 -1).但要求S到X的路径中相邻两条边颜色不一样. Limits Time Limit(ms): 1000 Memory Limit(MB): 256 |V|, |E|: [1, 10^5] X, S: [1, |V| ] L: [1, 10^9] |C|

Codeforces gym Hello 2015 Div1 E

Codeforces gym 100570 problem E (一种处理动态最长回文子串问题的方法) Problem 给一个长度为N的字符串S,字符集是'a'-'z'.进行Q次操作,操作分三种.一,修改位置X的字符为C:二,查询以P位置为中心的最长回文子串的长度,并输出:三,查询以P与P+1的中间位置为中心的最长回文子串的长度,并输出. More 第二种操作子串长度为奇数,一定存在:第三种操作子串长度为偶数,若不存在,输出 -1. Limits Time Limit(ms): 4000(1s足