• 문제 링크
27851번: Watching Mooloo
Bessie first buys a one-day subscription on day 1, spending $d+K = 1+3 = 4$ moonies. Bessie also buys a one-day subscription on day 10, spending $d+K = 1+3 = 4$ moonies. In total, Bessie spends 8 moonies.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
long n = read(), k = read();
long[] arr = new long[(int) n];
for (int i = 0; i < n; i++) arr[i] = read();
long sum = n + k;
for (int i = 1; i < n; i++) sum += Math.min(arr[i] - arr[i - 1] - 1, k);
bw.write(String.valueOf(sum));
bw.flush();
}
private static long read() throws IOException {
long c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}'Problem Solving > Baekjoon' 카테고리의 다른 글
| [백준] 1157 단어 공부 - Implementation / Java (0) | 2024.04.16 |
|---|---|
| [백준] 11597 Excellence - Greedy / Java (0) | 2024.04.15 |
| [백준] 21146 Rating Problems - Greedy / Java (0) | 2024.04.13 |
| [백준] 15001 Frog Leaps - Greedy / Java (0) | 2024.04.12 |
| [백준] 27106 Making Change - Greedy / Java (0) | 2024.04.11 |
댓글