Skip to content

Commit

Permalink
1999. 최대최소 - DP?
Browse files Browse the repository at this point in the history
  • Loading branch information
mwy3055 committed Jul 12, 2023
1 parent 7648ed9 commit 9209638
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions baekjoon/cpp/c++/1999.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <bits/stdc++.h>

int n, b, k;
int matrix[251][251];
int rmax[251][251], rmin[251][251];

void getinput()
{
std::cin >> n >> b >> k;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
std::cin >> matrix[i][j];
}
}
}

void preprocess()
{
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n - b + 1; j++)
{
int max = 0, min = 251;
for (int k = 0; k < b; k++)
{
max = std::max(max, matrix[i][j + k]);
min = std::min(min, matrix[i][j + k]);
}
rmax[i][j] = max;
rmin[i][j] = min;
}
}
}

int solve(int i, int j)
{
int max = 0, min = 251;
for (int k = 0; k < b; k++)
{
max = std::max(max, rmax[i + k][j]);
min = std::min(min, rmin[i + k][j]);
}
return max - min;
}

void solve()
{
preprocess();

for (int a = 0; a < k; a++)
{
int i, j;
std::cin >> i >> j;
std::cout << solve(i, j) << '\n';
}
}

int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);

getinput();
solve();
}

0 comments on commit 9209638

Please sign in to comment.