Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BOJ 1700 멀티탭 스케줄링 #214

Open
Holdm2t1ght opened this issue Feb 13, 2023 · 1 comment
Open

BOJ 1700 멀티탭 스케줄링 #214

Holdm2t1ght opened this issue Feb 13, 2023 · 1 comment
Labels

Comments

@Holdm2t1ght
Copy link
Contributor

No description provided.

@Holdm2t1ght
Copy link
Contributor Author

package com.w5;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.stream.IntStream;

public class BOJ1700 {
	static int N, K, cnt;
	static int[] digitCnt, multiTab;
	static Queue<Integer> queue;
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("BOJ1700.txt")));
		//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		N = Integer.parseInt(st.nextToken());
		K = Integer.parseInt(st.nextToken());
		digitCnt = new int[100];
		queue = new LinkedList<>();
		
		st = new StringTokenizer(br.readLine());
		for(int i = 0;i<K;i++) {
			int digit = Integer.parseInt(st.nextToken());
			digitCnt[digit-1]++;
			queue.offer(digit);
		}
		
		cnt = 0;
		multiTab = new int[N];
		countingN();
		
		System.out.println(cnt);
		
	}
	private static void countingN() {
		int idx = 0;
		while(multiTab[N-1] == 0 && !queue.isEmpty()) {
			int digit = queue.poll();
			digitCnt[digit-1]--;
			if(!check(digit)) {
				multiTab[idx++] = digit;
			}
		}
		
		while(!queue.isEmpty()) {
			int digit = queue.poll();
			digitCnt[digit-1]--;
			if(!check(digit)) {
				multiTab[findMin()] = digit;
				cnt++;
			}
		}
		
	}
	private static int findMin() {
		int min = Integer.MAX_VALUE;
		int minTemp = 0;
		for(int i = 0;i<N;i++) {
			if(digitCnt[multiTab[i]-1] == 0) return i;
			else {
				if(!queue.isEmpty() && queue.peek() != multiTab[i]) {
					min = Math.min(min, digitCnt[multiTab[i]-1]);
					if(min == digitCnt[multiTab[i]-1]) minTemp = i;
				}
			}
		}
		return minTemp;
	}
	
	private static boolean check(int digit) {
		for(int i = 0;i<N;i++) {
			if(multiTab[i] == digit) return true;
		}
		return false;
	}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant