Neighborhood mean (202104-2) (100 decomposition method) (prefix and)

Posted by coder500 on Fri, 19 Nov 2021 07:51:28 +0100

Problem description

Question No.:202104-2
Test title:Neighborhood mean
time limit:1.0s
Memory limit:512.0MB
Problem Description:

Test background

After learning digital image processing, Dunton wants to denoise a gray image on his opponent. However, the image only has a lot of noise in the darker area. If you rashly denoise the whole image, you will erase the noise and blur the original image at the same time. Therefore, Dunton intends to use the neighborhood mean to judge whether a pixel is in a darker region, and then only denoise the pixels in a darker region.

Problem description

The length and width of the gray image to be processed are   n   Pixels, which can be represented as one   n × n   Size matrix   A. Each of these elements is a   [0,L)   An integer within the range, representing the gray value of the pixel at the corresponding position.
For any element in the matrix   Aij (0 ≤ I, J < n), whose neighborhood is defined as the sum of several nearby elements:

Neighbor(i,j,r)={Axy|0≤x,y<n and |x−i|≤r and |y−j|≤r}

An additional parameter is used here   r   To indicate   Aij   The specific range of nearby elements. By definition, it is easy to know   Neighbor(i,j,r)   At most   (2r+1)2   Elements.

If element   Aij   The average value of all elements in the neighborhood is less than or equal to a given threshold   t. We think that the pixel at the corresponding position of the element is in a darker area.
The following figure shows two examples. The darker area of the image on the left is shown in black in the image on the right, and the other areas are shown in white.

Given neighborhood parameters   r   And threshold   t. Try to count how many pixels in the input gray image are in the dark area.

Input format

Input total   n+1   that 's ok.

The first line of input contains four positive integers separated by spaces   n,L,r   and   t. Has the meaning set forth above.

Second to second   n+1   Row input matrix   A.
The first   i+2 (0 ≤ I < n) lines contain spaces separated   n   Integers, in order   Ai0,Ai1,⋯,Ai(n−1).

Output format

Outputs an integer representing the total number of pixels in the darker area of the input grayscale image.

sample input

4 16 1 6
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15

Data

sample output

7

Data

sample input

11 8 2 2
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 7 0 0 0 7 0 0 7 7 0
7 0 7 0 7 0 7 0 7 0 7
7 0 0 0 7 0 0 0 7 0 7
7 0 0 0 0 7 0 0 7 7 0
7 0 0 0 0 0 7 0 7 0 0
7 0 7 0 7 0 7 0 7 0 0
0 7 0 0 0 7 0 0 7 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0

Data

sample output

83

Data

Scale and agreement of evaluation cases

70%   The test data meet the requirements   n≤100,r≤10.

All test data meet   0<n≤600,0<r≤100   And   2≤t<L≤256.

 

//#include <bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<list>
#include<set>
#include<iomanip>
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<cassert>
#include<sstream>
#include<algorithm>
using namespace std;
const int mod=1e9+7;
typedef long long  ll;
#define ls (p<<1)
#define rs (p<<1|1)
#define mid (l+r)/2
#define over(i,s,t) for(register long long i=s;i<=t;++i)
#define lver(i,t,s) for(register long long i=t;i>=s;--i)
const int MAXN = 305;
const int INF = 0x3f3f3f3f;
const int N=5e4+7;
const int maxn=1e5+5;
const double EPS=1e-10;
const double Pi=3.1415926535897;
//inline double max(double a,double b){
//    return a>b?a:b;
//}
//inline double min(double a,double b){
//    return a<b?a:b;
//}
 
int xd[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int yd[8] = {1, 0, -1, 0, -1, 1, -1, 1};
 
//void Fire(){
//    queue<node> p;
//    p.push({fx,fy,0});
//    memset(fire, -1, sizeof(fire));
//    fire[fx][fy]=0;
//    while(!p.empty()){
//        node temp=p.front();
//        p.pop();
//        for(int i=0;i<8;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m||fire[x][y]!=-1){
//                continue;
//            }
//            fire[x][y]=temp.val+1;
//            p.push({x,y,temp.val+1});
//        }
//    }
//}
//int bfs(){
//    queue<node> p;
//    memset(vis, 0, sizeof(vis));
//    p.push({sx,sy,0});
//    while (!p.empty()) {
//        node temp=p.front();
//        vis[temp.x][temp.y]=1;
//        p.pop();
//        for(int i=0;i<4;i++){
//            int x=temp.x+xd[i];
//            int y=temp.y+yd[i];
//            if(x<0||x>=n||y<0||y>=m)  continue;
//            if(x==ex&&y==ey&&temp.val+1<=fire[x][y]) return temp.val+1;
//            if(vis[x][y]||temp.val+1>=fire[x][y]||a[x][y]=='#') continue;
//            p.push({x,y,temp.val+1});
//        }
//    }
//    return -1;
//}
int n,l,r,t;
ll a[610][610];
ll b[610][610];
//Prefix and calculation template
ll first(int i,int j){
    int sum=0;
    sum+=a[i][j];
    sum+=b[i][j-1];
    sum+=b[i-1][j];
    sum-=b[i-1][j-1];
    return sum;
}
ll tosum(int x1,int y1,int x2,int y2){
    ll sum=0;
    sum+=b[x2][y2];
    sum-=b[x2][y1-1];
    sum-=b[x1-1][y2];
    sum+=b[x1-1][y1-1];
    return sum;
}
double avg(int x,int y){
    int x1=max(1,x-r);
    int y1=max(1,y-r);
    int x2=min(x+r,n);
    int y2=min(y+r,n);
    int num=(x2-x1+1)*(y2-y1+1);
    return tosum(x1, y1, x2, y2)*1.0/num;
}
int main(){
    cin>>n>>l>>r>>t;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            cin>>a[i][j];
            b[i][j]=first(i,j);
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(avg(i, j)<=t){
                ans++;
            }
        }
    }
    cout<<ans;
}

Topics: Algorithm AI codeforce linear algebra AcWing