답은 알고리즘 뿐이야!

[BOJ 18111] 마인크래프트 본문

알고리즘/백준문제풀이

[BOJ 18111] 마인크래프트

skyde47 2020. 2. 10. 11:58

문제 출처 : https://www.acmicpc.net/problem/18111

 

풀이 :

 

완전 탐색 문제입니다.

각 높이별로 블록이 몇 개 있는지 저장한 후

각 높이를 탐색하여 조건에 맞게 계산한 후 최댓값을 뽑아내면 됩니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
using namespace std;
 
int n, m, b, a[257], result = 1e9, hei, i, j, ret, bb;
 
int main() {
    ios_base::sync_with_stdio(false);
    cout.tie(0);
    cin.tie(0);
    for (cin >> n >> m >> b; i < n*m; i++) {
        cin >> j;
        a[j]++;        
    }
    for (i = 256; ~i; i--) {
        ret = 0; bb = b;
        for (j = 256; j > i; j--) {
            ret += a[j] * (j - i) * 2;
            bb += a[j] * (j - i);
        }
        for (j = i - 1; ~j; j--) {
            ret += a[j] * (i - j);
            bb -= a[j] * (i - j);
        }
        if (bb < 0)continue;
        if (result > ret) {
            result = ret;
            hei = i;
        }
    }
    cout << result << ' ' << hei;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter

 

 

Comments