-
[18111] 마인크래프트BOJ 2022. 4. 3. 18:28
https://www.acmicpc.net/problem/18111
18111번: 마인크래프트
팀 레드시프트는 대회 준비를 하다가 지루해져서 샌드박스 게임인 ‘마인크래프트’를 켰다. 마인크래프트는 1 × 1 × 1(세로, 가로, 높이) 크기의 블록들로 이루어진 3차원 세계에서 자유롭게
www.acmicpc.net
입력된 블럭들중 가장 작은것부터, 가장 큰것까지만 탐색하면 그안에 항상 최적해가 있음을 기대할 수 있다.
#include <bits/stdc++.h>using namespace std;#ifdef ONLINE_JUDGEconstexpr bool local = false;#elseconstexpr bool local = true;#endifusing ll = long long;using pi = pair<ll, ll>;constexpr ll INF = INT_MAX;ll n, m, b, sum, ansCost, ansHeight, a[501][501];int main(void) {if (!local) ios_base::sync_with_stdio(0), cin.tie(0);cin >> n >> m >> b;ll i, j, s = INF, e = 0;for (i = 0; i < n; i++) {for (j = 0; j < m; j++) {ll x;cin >> x;sum += x;s = min(x, s);e = max(x, e);a[i][j] = x;}}ansCost = INF;for (ll x = s; x <= e; x++) {if ((sum + b) < (x * n * m)) break;ll cost = 0, block = b;for (i = 0; i < n; i++) {for (j = 0; j < m; j++) {if (a[i][j] < x) {ll cur = x - a[i][j];cost += cur;block -= cur;} else if (a[i][j] > x) {ll cur = a[i][j] - x;cost += (cur + cur);block += cur;}}}if (block < 0) continue;if (cost <= ansCost) {ansCost = cost;ansHeight = x;}}cout << ansCost << ' ' << ansHeight;return 0;}
'BOJ' 카테고리의 다른 글
[5525] IOIOI (0) 2022.04.07 [9375] 패션왕 신해빈 (0) 2022.04.07 [1302] 베스트셀러 (0) 2022.04.01 [1940] 주몽 (0) 2022.03.30 [3649] 로봇 프로젝트 (0) 2022.03.30