문제

풀이
모든 땅의 높이가 각각 0,1,2,...,254,255,256으로 맞춰지는 시간을 구하여 그 중 최소 시간과 최소 시간이 나올 때의 땅의 높이를 구해 출력하면 된다.
#include <iostream>
#include <vector>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n, m, b;
int board[501][501];
int minTime = 0x7f7f7f7f;
int maxH = -1;
cin >> n >> m >> b;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> board[i][j];
for (int height = 0; height <= 256; height++) {
int removeBlock = 0, deployBlock = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
int h = board[i][j] - height;
if (h > 0) removeBlock += h;
else if (h < 0) deployBlock -= h;
}
}
if (removeBlock + b >= deployBlock) {
int t = 2 * removeBlock + deployBlock;
if (t <= minTime) {
minTime = t;
maxH = height;
}
}
}
cout << minTime << " " << maxH;
return 0;
}
'백준 > 기타' 카테고리의 다른 글
[백준/C++] 1992번: 쿼드트리 (0) | 2023.05.22 |
---|---|
[백준/C++] 2667번 : 단지번호붙이기 (1) | 2023.05.18 |
[백준/C++] 10773번 : 제로 (0) | 2023.04.10 |
9012번: 괄호 [C++] (0) | 2023.04.10 |
4949번: 균형잡힌 세상 [C++] (0) | 2023.04.10 |