반응형
https://www.acmicpc.net/problem/18111
18111번: 마인크래프트
팀 레드시프트는 대회 준비를 하다가 지루해져서 샌드박스 게임인 ‘마인크래프트’를 켰다. 마인크래프트는 1 × 1 × 1(세로, 가로, 높이) 크기의 블록들로 이루어진 3차원 세계에서 자유롭게
www.acmicpc.net
높이를 0부터 256까지 모두 확인해주어야 문제를 해결할 수 있다.
초기 배열의 값들을 기준으로 높이를 설정해 최소시간을 구했는데
초기 배열에 존재하지 않는 값이 최소시간의 땅의 높이가 될 수 있다.
#include <bits/stdc++.h>
using namespace std;
int n, m, b, arr[501][501];
int height=-1, cnt = INT_MAX;
void func(int x) {
int test_b = b;
int test_cnt = 0;
int test[501][501];
copy(&arr[0][0], &arr[0][0] + 501 * 501, &test[0][0]);
//파내기 먼저 해줘서 b를 확보한다
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (test[i][j] > x) {
test_cnt += 2 * (test[i][j] - x);
test_b += (test[i][j] - x);
if (test_cnt > cnt) return;
}
}
}
//쌓기
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (test[i][j] < x) {
test_b -= (x - test[i][j]);
if (test_b < 0) return;
test_cnt += (x - test[i][j]);
if (test_cnt > cnt) return;
}
}
}
if (cnt > test_cnt) {
cnt = test_cnt;
height = x;
}
else if (cnt == test_cnt) {
if (x > height) height = x;
}
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m >> b;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> arr[i][j];
}
}
for (int i = 0; i <= 256; i++) {
func(i);
}
cout << cnt << ' ' << height << '\n';
return 0;
}

반응형
'Algorithm > problem' 카테고리의 다른 글
백준 16168번 : 퍼레이드 - 오일러 회로 C++ (0) | 2022.02.28 |
---|---|
백준 1199번: 오일러 회로 시간초과 해결 (0) | 2022.02.28 |
백준 1948번: 임계경로 - 위상정렬 C++ (0) | 2022.02.26 |
백준 2848번 : 알고스팟어 - 위상정렬 C++ 코드 (0) | 2022.02.25 |
백준 2056번: 작업 - 위상정렬 dp C++ 코드 (0) | 2022.02.25 |