This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#include "quality.h"
using namespace std;
const int MAXN=3005;
const int INF=1e9+5;
int n,m;
int h,w;
int half;
int pref[MAXN][MAXN];
int minPoss1[MAXN][MAXN];
int minPoss2[MAXN][MAXN];
bool check(int x,int q[][3001])
{
pref[0][0]=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(i>0 && j>0) pref[i][j]=pref[i-1][j]+pref[i][j-1]-pref[i-1][j-1];
else if(i>0) pref[i][j]=pref[i-1][j];
else if(j>0) pref[i][j]=pref[i][j-1];
if(q[i][j]<x) pref[i][j]++;
if(i>=h-1 && j>=w-1)
{
int a1=0,a2=0,b=0;
if(i>=h) a1=pref[i-h][j];
if(j>=w) a2=pref[i][j-w];
if(i>=h && j>=w) b=pref[i-h][j-w];
if(pref[i][j]-a1-a2+b>=half) return 1;
}
}
}
return 0;
}
int find_ans(int x,int q[][3001])
{
deque<int> dq;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{//if(q[i][j]==x) cout<<i<<" "<<j<<endl;
while(!dq.empty() && dq.front()<=j-w) dq.pop_front();
if(q[i][j]>=x)
{
while(!dq.empty() && q[i][j]<q[i][dq.back()]) dq.pop_back();
dq.push_back(j);
}
if(!dq.empty()) minPoss1[i][j]=q[i][dq.front()]; //
else minPoss1[i][j]=INF;
//if(minPoss[i][j]==x) cout<<i<<" "<<j<<endl;
}
while(!dq.empty()) dq.pop_back();
}
for(int j=0;j<m;j++)
{
for(int i=0;i<n;i++)
{
while(!dq.empty() && dq.front()<=i-h) dq.pop_front();
if(minPoss1[i][j]>=x)
{
while(!dq.empty() && minPoss1[i][j]<minPoss1[dq.back()][j]) dq.pop_back();
dq.push_back(i);
}
if(!dq.empty()) minPoss2[i][j]=minPoss1[dq.front()][j];
else minPoss2[i][j]=INF;
//if(minPoss[i][j]==x) cout<<i<<" "<<j<<" "<<dq.front()<<endl;
}
while(!dq.empty()) dq.pop_back();
}
int ans=INF;
pref[0][0]=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(i>0 && j>0) pref[i][j]=pref[i-1][j]+pref[i][j-1]-pref[i-1][j-1];
else if(i>0) pref[i][j]=pref[i-1][j];
else if(j>0) pref[i][j]=pref[i][j-1];
if(q[i][j]<x) pref[i][j]++;
if(i>=h-1 && j>=w-1)
{
int a1=0,a2=0,b=0;
if(i>=h) a1=pref[i-h][j];
if(j>=w) a2=pref[i][j-w];
if(i>=h && j>=w) b=pref[i-h][j-w];
if(pref[i][j]-a1-a2+b>=half) ans=min(ans, minPoss2[i][j]); // cout<<pref[i][j]-a1-a2+b<<" "<<minPoss2[i][j]<<" "<<i<<" "<<j<<endl;
}
}
}
return ans;
}
int rectangle(int R,int C,int H,int W,int q[3001][3001])
{
n=R, m=C;
h=H, w=W;
half=h*w/2;
int l=1,r=n*m;
while(l<r)
{
int mid=(l+r)/2;
if(check(mid,q)) r=mid;
else l=mid+1;
}
//cout<<l<<endl;
return find_ans(l,q);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |