I am completely new to embedded programming, I'm examining the code below and trying to understand how it work, but I really got stuck.
#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
int n, m, q;
int g[N][N], dp[N][N], med[N][N];
int rows, cols, need, query[N*N], size;
int search(int L, int R, int query[], int size) {
if (L == R) return L;
int M = (L + R) / 2;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j)
dp[i+1][j+1] = -dp[i][j]+dp[i+1][j]+dp[i][j+1]+int(g[i][j] < M);
int tsize = 0;
for (int k = 0; k < size; ++k) {
int i = query[k] >> 16;
int j = query[k] & ((1 << 16) - 1);
int c = dp[i][j]-dp[i-rows][j]-dp[i][j-cols]+dp[i-rows][j-cols];
if (c <= need) {
query[tsize++] = query[k];
}
}
if (tsize > 0)
return search(M+1, R, query, tsize);
else
return search(L, M, query, size);
}
int main() {
scanf("%d%d%d", &n, &m, &q);
int mx = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < m; ++j) {
scanf("%d", &g[i][j]);
mx = max(mx, g[i][j]);
}
while (q--) {
scanf("%d%d", &rows, &cols);
need = rows * cols / 2;
size = 0;
for (int i = rows; i <= n; ++i)
for (int j = cols; j <= m; ++j)
query[size++] = (i << 16) | j;
printf("%d\n", search(0, mx+1, query, size)-1);
}
}
Aucun commentaire:
Enregistrer un commentaire