PS/๋ฐฑ์ค
[๋ฐฑ์ค] 2805 : ๋๋ฌด ์๋ฅด๊ธฐ - java [์๋ฐ]
mokhs
2022. 2. 26. 20:02
https://www.acmicpc.net/problem/2805
2805๋ฒ: ๋๋ฌด ์๋ฅด๊ธฐ
์ฒซ์งธ ์ค์ ๋๋ฌด์ ์ N๊ณผ ์๊ทผ์ด๊ฐ ์ง์ผ๋ก ๊ฐ์ ธ๊ฐ๋ ค๊ณ ํ๋ ๋๋ฌด์ ๊ธธ์ด M์ด ์ฃผ์ด์ง๋ค. (1 โค N โค 1,000,000, 1 โค M โค 2,000,000,000) ๋์งธ ์ค์๋ ๋๋ฌด์ ๋์ด๊ฐ ์ฃผ์ด์ง๋ค. ๋๋ฌด์ ๋์ด์ ํฉ์ ํญ์ M๋ณด
www.acmicpc.net
์ด ๊ธ์ ์ด๋ถํ์์ ์ด๋ฏธ ์๊ณ ์๋ ์ฌ๋์ ๋์์ผ๋ก ์์ฑํ๋ค.
- ์ด๋ถํ์์ ์ด์ฉํด ์ต์ ์ ๊ฐ์ ์ฐพ์๋ธ๋ค.
- ์ด๋ถํ์ ๋ฌธ์ ์์๋ ๊ตฌํด์ผํ๋ ๊ฐ์ ์ฐพ๊ณ ํด๋น ๊ฐ์ ๊ธฐ์ค์ผ๋ก ํ์ดํ๋ค. ๋ณดํต ์ ๋ต์ ์๊ตฌํ๋ ๊ฐ๊ณผ ์ผ์นํ๋ค.
- ํด๋น ๋ฌธ์ ์์๋ K(์ ๋จ๊ธฐ์ ์ค์ ํ ๋์ด์ ์ต๋๊ฐ)์ด ๊ธฐ์ค ๊ฐ์ด๋ค.
- ๋ฐ๋ผ์ ๋ณํํ๋ K๋ฅผ ๊ธฐ์ค์ผ๋ก totalSlicedHeight(์๋ ค์ง ๋๋ฌด ๊ธธ์ด์ ํฉ)์ด M(๊ฐ์ ธ๊ฐ์ผํ ๋๋ฌด ๊ธธ์ด)๋ณด๋ค ๋ถ์กฑํ ๋๋ K๋ฅผ ๊ฐ์์ํค๊ณ
- totalSlicedHeight๊ฐ M๋ณด๋ค ํด ๋๋ K๋ฅผ ์ฆ๊ฐ์ํจ๋ค.
- K์ ๋ํ ์ด๋ถํ์์ด ์ข ๋ฃ๋๋ฉด ํ์๋ K๊ฐ์ ์ถ๋ ฅํ๋ค. ์ด๋ถํ์์ด ์ข ๋ฃ๋๋ ์กฐ๊ฑด์ start > end ์ด๋ค.
์ฝ๋
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ๋๋ฌด_์๋ฅด๊ธฐ_2805 {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
String[] input = br.readLine().split(" ");
int N = Integer.parseInt(input[0]);
int M = Integer.parseInt(input[1]);
List<Integer> woodList = new ArrayList<>();
int maxHeight = 0;
for (String wood : br.readLine().split(" ")) {
int woodHeight = Integer.parseInt(wood);
woodList.add(woodHeight);
maxHeight = Math.max(maxHeight, woodHeight);
}
long start = 0;
long end = maxHeight;
while (start <= end) {
long mid = (start + end) / 2;
long totalSlicedHeight = 0;
for (Integer woodHeight : woodList) {
long slice = woodHeight - mid;
if (slice > 0) {
totalSlicedHeight += slice;
}
}
if (totalSlicedHeight < M) {
end = mid - 1;
} else {
start = mid + 1;
}
}
bw.write(Long.toString(end));
bw.close();
br.close();
}
}