问题 H: Pond(二维前缀和+二分)
The land of a park AtCoder is an N×N grid with east-west rows and north-south columns. The height of the square at the i-th row from the north and j-th column from the west is given as Ai,j.
Takahashi, the manager, has decided to build a square pond occupying K×K squares in this park.
To do this, he wants to choose a square section of K×K squares completely within the park whose median of the heights of the squares is the lowest. Find the median of the heights of the squares in such a section.
Here, the median of the heights of the squares in a K×K section is the height of the (⌊K2/2⌋+1)-th ighest square among the K2 squares in the section, where ⌊x⌋ is the greatest integer not exceeding x.
Constraints
1≤K≤N≤800
0≤Ai,j≤109
All values in input are integers.
Takahashi, the manager, has decided to build a square pond occupying K×K squares in this park.
To do this, he wants to choose a square section of K×K squares completely within the park whose median of the heights of the squares is the lowest. Find the median of the heights of the squares in such a section.
Here, the median of the heights of the squares in a K×K section is the height of the (⌊K2/2⌋+1)-th ighest square among the K2 squares in the section, where ⌊x⌋ is the greatest integer not exceeding x.
Constraints
1≤K≤N≤800
0≤Ai,j≤109
All values in input are integers.
输入
Input is given from Standard Input in the following format:
N K
A1,1 A1,2 … A1,N
A2,1 A2,2 … A2,N
:
AN,1 AN,2 … AN,N
N K
A1,1 A1,2 … A1,N
A2,1 A2,2 … A2,N
:
AN,1 AN,2 … AN,N
输出
Print the answer.
样例输入 Copy
【样例1】
3 2
1 7 0
5 8 11
10 4 2
【样例2】
3 3
1 2 3
4 5 6
7 8 9
样例输出 Copy
【样例1】
4
【样例2】
5
提示
样例1解释:
Let (i,j) denote the square at the i-th row from the north and j-th column from the west. We have four candidates for the 2×2 section occupied by the pond:
{(1,1),(1,2),(2,1),(2,2)},{(1,2),(1,3),(2,2),(2,3)},{(2,1),(2,2),(3,1),(3,2)},{(2,2),(2,3),(3,2),(3,3)}.
When K=2, since ⌊22/2⌋+1=3, the median of the heights of the squares in a section is the height of the 3-rd highest square, which is 5, 7, 5, 4 for the candidates above, respectively. We should print the lowest of these: 4.
Let (i,j) denote the square at the i-th row from the north and j-th column from the west. We have four candidates for the 2×2 section occupied by the pond:
{(1,1),(1,2),(2,1),(2,2)},{(1,2),(1,3),(2,2),(2,3)},{(2,1),(2,2),(3,1),(3,2)},{(2,2),(2,3),(3,2),(3,3)}.
When K=2, since ⌊22/2⌋+1=3, the median of the heights of the squares in a section is the height of the 3-rd highest square, which is 5, 7, 5, 4 for the candidates above, respectively. We should print the lowest of these: 4.
题意
给出一个 阶方阵,计算所有
阶方阵的中位数的最小值。
这里 阶方阵的中位数为:
个数中第
个大的数。
题解
二分中位数的值,将大于二分值的数记为 ,小于等于的记为
,作二维前缀和来枚举所有
阶方阵。
如果存在一个方阵满足其和小于等于 ,则当前二分值可行,设为上界,否则设为下界。
二分+二维前缀和
#include<iostream> #include<algorithm> #include<vector> using namespace std; const int maxn=1e3+100; int a[maxn][maxn]; int n,k; int judge(int x){ vector<vector<int>>sum(n+1,vector<int>(n+1)); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+(a[i][j]>x); } } for(int i=k;i<=n;i++){ for(int j=k;j<=n;j++){ int z=sum[i][j]-sum[i-k][j]-sum[i][j-k]+sum[i-k][j-k]; if(z<=k*k/2){ return 1; } } } return 0; } int main(){ cin>>n>>k; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf("%d",&a[i][j]); } } int l=0,r=1e9,ans=0; while(r>=l){ int mid=(l+r)/2; if(judge(mid)){ ans=mid; r=mid-1; } else{ l=mid+1; } } cout<<ans<<endl; }

浙公网安备 33010602011771号