Skip to content

Commit 97b95ac

Browse files
committed
[Silver V] Title: Ski Course Design, Time: 144 ms, Memory: 10164 KB -BaekjoonHub
1 parent 3382d19 commit 97b95ac

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Silver V] Ski Course Design - 9881
2+
3+
[๋ฌธ์ œ ๋งํฌ](https://www.acmicpc.net/problem/9881)
4+
5+
### ์„ฑ๋Šฅ ์š”์•ฝ
6+
7+
๋ฉ”๋ชจ๋ฆฌ: 10164 KB, ์‹œ๊ฐ„: 144 ms
8+
9+
### ๋ถ„๋ฅ˜
10+
11+
๋ธŒ๋ฃจํŠธํฌ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜, ์Šค์œ„ํ•‘
12+
13+
### ์ œ์ถœ ์ผ์ž
14+
15+
2026๋…„ 2์›” 28์ผ 15:17:51
16+
17+
### ๋ฌธ์ œ ์„ค๋ช…
18+
19+
<p>Farmer John has N hills on his farm (1 <= N <= 1,000), each with an integer elevation in the range 0 .. 100. In the winter, since there is abundant snow on these hills, FJ routinely operates a ski training camp.</p><p>Unfortunately, FJ has just found out about a new tax that will be assessed next year on farms used as ski training camps. Upon careful reading of the law, however, he discovers that the official definition of a ski camp requires the difference between the highest and lowest hill on his property to be strictly larger than 17. Therefore, if he shortens his tallest hills and adds mass to increase the height of his shorter hills, FJ can avoid paying the tax as long as the new difference between the highest and lowest hill is at most 17.</p><p>If it costs x^2 units of money to change the height of a hill by x units, what is the minimum amount of money FJ will need to pay? FJ is only willing to change the height of each hill by an integer amount.</p>
20+
21+
### ์ž…๋ ฅ
22+
23+
<ul><li>Line 1: The integer N.</li><li>Lines 2..1+N: Each line contains the elevation of a single hill.</li></ul>
24+
25+
### ์ถœ๋ ฅ
26+
27+
<ul><li>Line 1: The minimum amount FJ needs to pay to modify the elevations of his hills so the difference between largest and smallest is at most 17 units.</li></ul>
28+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
const input = fs
5+
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt")
6+
.toString()
7+
.trim()
8+
.split("\n");
9+
10+
/**
11+
* ๋ชฉํ‘œ: ๊ฐ€์žฅ ๋†’์€ ์–ธ๋•๊ณผ ๊ฐ€์žฅ ๋‚ฎ์€ ์–ธ๋•์˜ ์ฐจ์ด๊ฐ€ ์ตœ๋Œ€ 17์ด ๋˜๊ฒŒ ์–ธ๋•๋“ค์˜ ๋†’์ด ์กฐ์ •
12+
* ์ด๋•Œ ๋“œ๋Š” ์ตœ์†Œ๋น„์šฉ = ?
13+
* ์–ด๋–ค ์–ธ๋•์˜ ๋†’์ด๋ฅผ x๋งŒํผ ๋ณ€๊ฒฝํ•  ๋•Œ ๋“œ๋Š” ๋น„์šฉ: x^2
14+
* ๋ชจ๋“  ์–ธ๋• ๋†’์ด๋Š” ์ •์ˆ˜๋กœ๋งŒ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ
15+
* ์ž…๋ ฅ: 1๋ฒˆ์งธ ์ค„์—๋Š” ์ •์ˆ˜ N, 2๋ฒˆ์งธ ์ค„๋ถ€ํ„ฐ N+1๋ฒˆ์งธ ์ค„๊นŒ์ง€๋Š” ๊ฐ ์–ธ๋•์˜ ๋†’์ด
16+
* 1<=N<=1000, 0<=height<=100
17+
*/
18+
19+
const N = Number(input[0]);
20+
const hArr = input.slice(1, N + 1).map(Number);
21+
22+
let minCost = Infinity;
23+
24+
for (let low = 0; low <= 83; low++) {
25+
let currentCost = 0;
26+
const high = low + 17; //๊ฐ€๋Šฅํ•œ ์ตœ๋Œ€๋†’์ด: low+17
27+
28+
for (let i = 0; i < N; i++) {
29+
const height = hArr[i];
30+
if (height < low) {
31+
currentCost += (low - height) ** 2;
32+
} else if (height > high) {
33+
currentCost += (height - high) ** 2;
34+
}
35+
}
36+
minCost = Math.min(minCost, currentCost);
37+
}
38+
39+
console.log(minCost);

0 commit comments

Comments
ย (0)