#include<bits/stdc++.h>
#include "quality.h"
using namespace std;
const int N = 9E6 + 5;
int median;
int t[4*N];
int pos,numb,L,R,ans = INT_MAX;
void upd (int v = 1,int tl = 0,int tr = numb) {
if (tl == tr) {
if (t[v]) t[v] = 0;
else t[v] = 1;
return;
}
int tm = (tl +tr)/2;
if (pos <= tm)
upd (v*2,tl,tm);
else upd (v*2|1,tm+1,tr);
t[v] = t[v*2] + t[v*2 + 1];
}
int get (int v = 1, int tl = 0, int tr = numb,int F = median) {
if (tl == tr) return tl;
int tm = (tl + tr)/2;
if (F <= t[v*2]) return get(v*2,tl,tm,F);
F -= t[v*2];
return get(v*2|1,tm+1,tr,F);
}
int rectangle(int R, int C, int H, int W, int Q[3001][3001]) {
numb = H*W;
median = (numb + 1)/2;
for (int i = 0; i < H; i++)
for (int j = 0; j < W; j++) {
pos = Q[i][j];
upd();
}
int r = W - 1, l = 0, h = 0, d = H - 1;
int dir = 0; // 0 - r 1 - d 2 - l
while (true) {
ans = min(ans,get());
if (dir == 0) {
if (r != C - 1) {
for (int i = h; i <= d; i++) {
pos = Q[i][l];
upd();
pos = Q[i][r + 1];
upd();
}
r++;
l++;
}
else if (d != R - 1){
dir = 1;
for (int i = l; i <=r ; i++) {
pos = Q[h][i];
upd();
pos = Q[d + 1][i];
upd();
}
h++;
d++;
}
else break;
}
else {
if (dir == 1) {
if (l != 0) {
dir = 2;
for (int i = h; i <= d; i++) {
pos = Q[i][r];
upd();
pos = Q[i][l-1];
upd();
}
r--;
l--;
}
else if (r != C - 1) {
dir = 0;
for (int i = h; i <= d; i++) {
pos = Q[i][l];
upd();
pos = Q[i][r + 1];
upd();
}
r++;
l++;
}
else if (d != R - 1) {
for (int i = l; i <=r ; i++) {
pos = Q[h][i];
upd();
pos = Q[d + 1][i];
upd();
}
h++;
d++;
}
else break;
}
else {
if (l != 0) {
for (int i = h; i <= d; i++) {
pos = Q[i][r];
upd();
pos = Q[i][l-1];
upd();
}
r--;
l--;
}
else if (d != R - 1) {
dir = 1;
for (int i = l; i <=r ; i++) {
pos = Q[h][i];
upd();
pos = Q[d + 1][i];
upd();
}
h++;
d++;
}
else break;
}
}
}
return ans;
//return R*C/2;
}
//static int R,C,H,W,Q[3001][3001],i,j,ans;
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |