https://www.acmicpc.net/problem/1700
1700번: 멀티탭 스케줄링
기숙사에서 살고 있는 준규는 한 개의 멀티탭을 이용하고 있다. 준규는 키보드, 헤어드라이기, 핸드폰 충전기, 디지털 카메라 충전기 등 여러 개의 전기용품을 사용하면서 어쩔 수 없이 각종 전
www.acmicpc.net
저는 각각의 전기용품마다 큐를 만들어 해결하였습니다.
해당 큐를 통해 다음 인덱스가 어디인지 확인하고 가장 먼 인덱스를 갖고 있는 멀티탭에 있는 전기용품을 빼고 현재 것으로 교체하여 문제를 해결하였습니다.
package BOJ.greedy;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.function.Function;
public class BOJ_1700 {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Function<String,Integer> stoi = Integer::parseInt;
StringTokenizer st = new StringTokenizer(br.readLine());
int n = stoi.apply(st.nextToken());
int k = stoi.apply(st.nextToken());
int[] input = new int[k];
st = new StringTokenizer(br.readLine());
for(int i = 0 ; i < k ; i++){
input[i] = stoi.apply(st.nextToken());
}
int result = cal(input,n,k);
System.out.println(result);
}
private static final int INF = 987654321;
private static int cal(int[] input, int n, int k) {
Queue<Integer>[] q = new Queue[k+1];
for(int i = 0 ; i <= k ; i++){
q[i] = new LinkedList<>();
}
for(int i = 0 ; i < k ; i++){
q[input[i]].add(i);
}
int result = 0;
int cnt = 0;
boolean[] nowUsed = new boolean[k+1];
for(int i = 0 ; i < k ; i++){
int now = input[i];
if(nowUsed[now]){
q[now].poll();
continue;
}
if(cnt < n){
cnt++;
nowUsed[now] = true;
q[now].poll();
}else{
int value = 0;
int maxIndex = 0;
for(int j = 1 ; j <= k ; j++){
if(!nowUsed[j]){
continue;
}
if(q[j].isEmpty() || maxIndex < q[j].peek()){
value = j;
if(q[j].isEmpty()){
maxIndex = INF;
break;
}else{
maxIndex = q[j].peek();
}
}
}
nowUsed[value] = false;
nowUsed[now] = true;
q[now].poll();
result++;
}
}
return result;
}
}
'알고리즘' 카테고리의 다른 글
백준 2096번 내려가기 (JAVA) (0) | 2023.07.11 |
---|---|
백준 13904번 과제 (JAVA) (0) | 2023.07.10 |
백준 20922번 겹치는 건 싫어 (JAVA) (0) | 2023.07.08 |
백준 2579번 계단 오르기 (JAVA) (0) | 2023.07.07 |
백준 1806번 부분합 (JAVA) (0) | 2023.07.06 |