# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
240978 | ant101 | Quality Of Living (IOI10_quality) | C++14 | 0 ms | 0 KiB |
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 <iostream>
#include <algorithm>
#include <cstring>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <map>
#include <stack>
#include <queue>
#include <assert.h>
#include <limits>
#include <cstdio>
using namespace std;
//#define RDEBUG 1
#ifdef RDEBUG
#define D(x) x
#else
#define D(x)
#endif
#define inf 0x7fffffff
#define MOD 1000000007
#define f first
#define s second
#define N 3005
typedef long long ll;
ll add(ll a, ll b) {
a += b;
if(a >= MOD) {
a -= MOD;
}
return a;
}
ll sub(ll a, ll b) {
a -= b;
if(a < 0) {
a += MOD;
}
return a;
}
ll mul(ll a, ll b) {
return (a * b)%MOD;
}
void add_self(ll& a, ll b) {
a = add(a, b);
}
void sub_self(ll& a, ll b) {
a = sub(a, b);
}
void mul_self(ll& a, ll b) {
a = mul(a, b);
}
const ll MAXN = 200010;
using namespace std;
typedef pair<int, int> pii;
int n, m, r, c, mat[N][N], bit[N][N];
int sum(int xi, int yi, int xii, int yii)
{
return bit[xii][yii] - bit[xi - 1][yii] - bit[xii][yi - 1] + bit[xi - 1][yi - 1];
}
pii v[N*N];
pii verif(int x, int y)
{
bool A = false, B = false;
for(int i = 1; i <= r; i++)
{
for(int j = 1; j <= c; j++)
{
int xf = i + n - 1, yf = j + m - 1;
if(xf <= 0 || yf <= 0 || xf > r || yf > c ) continue;
int S = sum(i, j, xf, yf);
if(S == (n*m - 1)/2 && (i <= x && x <= xf && j <= y && y <= yf)) A = true;
if(S > (n*m - 1)/2) B = true;
}
}
return pii(A, B);
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0);
cin>>r>>c>>n>>m;
for(int i = 1; i <= r; i++)
for(int j = 1; j <= c; j++)
cin>>mat[i][j], v[mat[i][j] - 1] = pii(i, j);
int ini = 0, fim = r*c - 1, mid, best = 2000000000;
while(fim >= ini)
{
mid = (ini + fim)/2;
memset(bit, 0, sizeof bit);
for(int i = 0; i < mid; i++)
{
pii val = v[i];
bit[val.f][val.s] = 1;
}
for(int i = 1; i <= r; i++)
for(int j = 1; j <=c ; j++)
bit[i][j] += bit[i - 1][j] + bit[i][j - 1] - bit[i - 1][j - 1];
pii V = verif(v[mid].f, v[mid].s);
if(V.f && V.s)
{
best = min(best, mid);
fim = mid - 1;
}
else if(V.f && !V.s)
{
best = min(best, mid);
ini = mid + 1;
}
else if(!V.f && V.s) fim = mid - 1;
else ini = mid + 1;
}
cout<<best + 1<<"\n";
}