[Algorithm/Java] 백준 3985번 - 롤 케이크
https://www.acmicpc.net/problem/3985
🔍 문제 풀이
문제 도식화
알고리즘 선택
배운 점
💻 전체 코드
// 8시 15분~11:45
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int l = Integer.parseInt(br.readLine());
int n = Integer.parseInt(br.readLine());
int expectMax = Integer.MIN_VALUE;
int expectMaxIdx = 0;
int realMax = Integer.MIN_VALUE;
int realMaxIdx = 0;
int[] cake = new int[l+1];
for(int i=1; i<=n; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
int p = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
// 기댓값
int temp = k - p + 1;
if(temp > expectMax){
expectMax = temp;
expectMaxIdx = i;
}
// 실제값
int cnt = 0;
for(int j=p; j<=k; j++){
if(cake[j] == 0) {
cake[j] = 1;
cnt ++;
}
}
if(cnt > realMax){
realMax = cnt;
realMaxIdx = i;
}
}
System.out.println(expectMaxIdx);
System.out.println(realMaxIdx);
}
}
댓글남기기