본문 바로가기

알고리즘/구현 및 시뮬레이션

[B9881] - Ski Course Design

https://www.acmicpc.net/problem/9881

산의 개수가 최대 1000개, 산의 높이는 0부터 100까지 이므로 최대 1000*101번의 연산만 하면 되므로

기준이 되는 산의 높이를 0부터 100까지 돌려본다. 높이기준이 i라고 했을 때 높이가 i+17일 때 까지는 

세금을 내지 않는다. 즉 산의 높이 중 i보다 작은 경우와 i+17보다 큰 경우의 비용만 구해서 더해주고

그 비용 중 가장 작은 값을 출력한다. 


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
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n;
    cin >> n;
    vector<int>a(n);
    int ans = 2147483647;
    for (int i = 0; i < n; i++)
        cin >> a[i];
    for (int height = 0; height <= 100; height++)
    {
        int temp = 0;
        for (int i = 0; i < n; i++)
        {
            if (a[i]<height)
                temp += (height - a[i])*(height - a[i]);
            else if (a[i]>height+17)
                temp += (17+height - a[i])*(17+height - a[i]);
        }
        if (temp < ans)
            ans = temp;
    }
    cout << ans << '\n';
}
cs

'알고리즘 > 구현 및 시뮬레이션' 카테고리의 다른 글

[B16113] - 시그널  (0) 2018.09.26
[B14467] - 소가 길을 건너간 이유1  (0) 2018.09.24
[B9882] - Balanced Teams  (0) 2018.09.24
[B10657] - Cow Jog  (0) 2018.09.23
[B9573] - Combination Lock  (0) 2018.09.23