https://www.acmicpc.net/problem/18111
ํ์ด๊ณผ์
ํด๋น ๋ฌธ์ ์์ ์ ๊ฒฝ์จ์ผํ๋ ๋ถ๋ถ์ ์ด์ ๊ฐ๋ค.
1. ‘๋ ๊ณ ๋ฅด๊ธฐ’ ์์ ์ ๊ฑธ๋ฆฌ๋ ์ต์ ์๊ฐ๊ณผ ๊ทธ ๊ฒฝ์ฐ ๋ ์ ๋์ด๋ฅผ ์ถ๋ ฅํ์์ค.
2. ๋ ์ ๋์ด๋ 256๋ธ๋ก์ ์ด๊ณผํ ์ ์์ผ๋ฉฐ, ์์๊ฐ ๋ ์ ์๋ค.
3. ๋ต์ด ์ฌ๋ฌ ๊ฐ ์๋ค๋ฉด ๊ทธ์ค์์ ๋ ์ ๋์ด๊ฐ ๊ฐ์ฅ ๋์ ๊ฒ์ ์ถ๋ ฅํ์์ค.
๋ฌธ์ ์์๋ ๋ ๊ณ ๋ฅด๊ธฐ๋ฅผ ํ๋ฉด์ ๊ฑธ๋ฆฌ๋ ์ต์์๊ฐ๊ณผ ๊ทธ ๋ ์ ๋์ด๋ฅผ ๊ตฌํด์ผํ๋ค๊ณ ํ๋ค.
๋ ์ ๋์ด์ ๋ฒ์๊ฐ 0 ~ 256 ๊น์ง๋ก ์ ์ ๋ฒ์ ๋ด์ ์ ํ์ด ๋์ด์์ผ๋ ๋ชจ๋ ๋ ๋์ด๋ฅผ ๋ฐ๋ณต๋ฌธ์ ๋๋ฉฐ, targetHeight๋ฅผ ๊ธฐ์ค์ ๋์ด๋ก ๋ชจ๋ ๋ ์ ๋ง๋ค์ด๋ณด๊ณ ๋ธ๋ญ๊ฐ์๋ด์ ๊ฐ๋ฅํ์ง, ์๊ฐ์ด ๊ฐ์ฅ ์ ์์ง๋ฅผ ํ์ธํ๋ฉฐ ๊ฒฐ๊ณผ๊ฐ์ ๊ตฌํ์๋ค.
code
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <climits>
using namespace std;
int main() {
int n, m, b, land;
cin >> n >> m >> b;
unordered_map<int, int> frequency;
for (int i = 0; i < n * m; i++) {
cin >> land;
frequency[land]++;
}
int inventory, time;
int minTime = INT_MAX, bestHigh = 0;
for (int targetHeight = 0; targetHeight <= 256; targetHeight++) {
time = 0;
inventory = b;
for(const auto& [height, freq] : frequency) {
if (height < targetHeight) {
int blocksNeeded = (targetHeight - height) * freq;
inventory -= blocksNeeded;
time += blocksNeeded;
} else if (height > targetHeight) {
int blocksRemoved = (height - targetHeight) * freq;
inventory += blocksRemoved;
time += blocksRemoved * 2;
}
}
if (0 <= inventory) {
if (minTime > time || (minTime == time && bestHigh < targetHeight)) {
minTime = time;
bestHigh = targetHeight;
}
}
}
cout << minTime << " " << bestHigh;
return 0;
}
ํ๊ธฐ
์ฒ์์๋ ์๊ฐ์ ์ต๋ํ ์ ์ฝํ๊ณ ์ถ์ด์ ํ์ฌ ๊ฐ์ผ๋ก ๋ค์ด์จ ๋
๋ค์ ๋ฐํ์ผ๋ก ์ ๋ ฌ ํ ๋ฐ๋ณต๋ฌธ์ ๋๋ ธ์๋๋ฐ. ๋ก์ง์ด ํจ์ฌ ๋ณต์กํด์ง๊ณ ,
๋ฐ๋ก๋ค๋ ์๋นํ ๋ง์์๋ค.
๋ฌธ์ ์ ์ ๊ทผ ๋ฐฉ์์ ๋ค๋ฅด๊ฒ ์๊ฐํ๋ ๋ฅ๋ ฅ์ ๊ธธ๋ฌ์ผํ๋๊ฑฐ ๊ฐ๋ค.
'Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 15829๋ฒ - Hashing (C++) (1) | 2024.11.11 |
---|---|
[๋ฐฑ์ค] 2108๋ฒ - ํต๊ณํ (C++) (0) | 2024.11.10 |