zoukankan      html  css  js  c++  java
  • [模拟,英语阅读] Codeforces 549D Haar Features

    题目:https://codeforces.com/contest/549/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 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
    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的特征表格,每个格要么是B要么是W,现在你有一个全为0的n*m的表格,目标是使格中的值与特征表格对应,B对应-1,W对应1,
    为了达成这个目标你有一个操作,可以在表格中选定一个坐标,使从表格左上角到这个坐标形成的矩形中的格都加上你想要加的一个数,
    问为了达成目标的最少操作次数是多少

    思路:

    这个题在训练时硬是没看懂(语文英语水平太弱了...TAT),当时看题时就在想一些问题:1,这个操作到底是干什么的? 2.那个prefix rectangle是怎么样的?3.最后要达成的目标是什么?回答:1.这个操作是选一个坐标,使得表格左上角到这个坐标的数全都加上一个选定的数.2.一个prefix rectangle指的是矩形左上角固定在表格左上角,并得到到一个坐标作为右下角形成的矩形.3.把一个初始值为0的表格,通过最少次数的上面那种操作,使得表格中的值对应特征矩阵中B为-1,W为1(为什么???这个是Note里说的,Note也可以作为条件啊)考虑尽可能少的操作,因为prefix rectangle左上角固定了,我们可以从右下角开始逐行从右到左从下到上检测是否需要改变,这样就不会有无用的重复操作.

    感觉这是一道英语阅读题...我太菜了TAT
     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 const int amn=1e2+5;
     4 char mp[amn][amn];
     5 int a[amn][amn];
     6 int main(){
     7     int n,m,ans,typ;
     8     ios::sync_with_stdio(0);
     9     while(cin>>n>>m){
    10         memset(a,0,sizeof a);
    11         for(int i=1;i<=n;i++){
    12             for(int j=1;j<=m;j++){
    13                 cin>>mp[i][j];
    14             }
    15         }
    16         ans=0;
    17         for(int i=n;i>=1;i--){
    18             for(int j=m;j>=1;j--){
    19                 typ=((mp[i][j]=='W')?1:-1);
    20                 if(a[i][j]!=typ){
    21                     ans++;
    22                     for(int k=1;k<=i;k++){
    23                         for(int l=1;l<=j;l++){
    24                             a[k][l]+=typ-a[i][j];
    25                         }
    26                     }
    27                 }
    28             }
    29         }
    30         printf("%d
    ",ans);
    31     }
    32 }
    33 /**
    34 给你一个n*m的特征表格,每个格要么是B要么是W,现在你有一个全为0的n*m的表格,目标是使格中的值与特征表格对应,B对应-1,W对应1,
    35 为了达成这个目标你有一个操作,可以在表格中选定一个坐标,使从表格左上角到这个坐标形成的矩形中的格都加上你想要加的一个数,
    36 问为了达成目标的最少操作次数是多少
    37 这个题在训练时硬是没看懂(语文英语水平太弱了...TAT),当时看题时就在想一些问题:1,这个操作到底是干什么的? 2.那个prefix rectangle是怎么样的?
    38 3.最后要达成的目标是什么?
    39 回答:
    40 1.这个操作是选一个坐标,使得表格左上角到这个坐标的数全都加上一个选定的数.
    41 2.一个prefix rectangle指的是矩形左上角固定在表格左上角,并得到到一个坐标作为右下角形成的矩形.
    42 3.把一个初始值为0的表格,通过最少次数的上面那种操作,使得表格中的值对应特征矩阵中B为-1,W为1(为什么???这个是Note里说的,Note也可以作为条件啊)
    43 考虑尽可能少的操作,因为prefix rectangle左上角固定了,我们可以从右下角开始逐行从右到左从下到上检测是否需要改变,这样就不会有无用的重复操作
    44 感觉这是一道英语阅读题...我太菜了TAT
    45 **/
  • 相关阅读:
    w3c盒子模型与ie盒子模型
    前端入门
    连接数据库好用工具
    前端开发工具
    刚发现的取色工具
    使用val()另一个妙用------选中select/checkbox/radio的值
    z-index的妙用
    react生命周期函数
    react中虚拟dom的diff算法
    React中的虚拟DOM
  • 原文地址:https://www.cnblogs.com/Railgun000/p/11537481.html
Copyright © 2011-2022 走看看