9576번: 책 나눠주기

백준이는 방 청소를 하면서 필요 없는 전공 서적을 사람들에게 나눠주려고 한다. 나눠줄 책을 모아보니 총 N권이었다. 책이 너무 많기 때문에 백준이는 책을 구분하기 위해 각각 1부터 N까지의

www.acmicpc.net

 

 


 

 

  • 생각

 

1. 각각의 테스트케이스 별로 a와 b 사이에 있는 책을 줄 수 있으면 주고 아니면 못 준다. array에 배열을 넣은 뒤 정렬 해준 다음 a부터 b까지 보면서 책을 빌려주지 않았으면 빌려주면 될 것 같다. 이걸로 시간 초과가 뜨면 이분탐색?을 써보는 방법도 괜찮을 것 같다.

 

이분 탐색을 하지않아도 a랑 b를 잘 정렬하면 시간초과가 뜨지 않는다. 내가 쓴 정렬 방법은 b를 기준으로 오름차순 정렬하고 b가 같을 땐 a로 오름차순 정렬을해서 책 빌리는 번호가 가장 작은 것 먼저 빌려주게 하였다.

 

 

 

 

  • 코드

 

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Comparator;
import java.util.InputMismatchException;

public class Main {
	static int testcase, N, M;
	static int[][] book;
	static boolean[] check;
	static StringBuilder sb;	

	public static void main(String[] args) throws Exception {
		SetData();
		System.out.println(sb);
	}

	private static void SetData() throws Exception {
		InputReader in = new InputReader(System.in);
		
		testcase = in.nextInt();
		sb = new StringBuilder();
		
		for(int i = 0; i < testcase; i++) {
			N = in.nextInt();
			M = in.nextInt();
			
			check = new boolean[N];
			book = new int[M][2];
			for(int j = 0; j < M; j++) {
				book[j][0] = in.nextInt();
				book[j][1] = in.nextInt();
			}
			
	        Arrays.sort(book, new Comparator<int[]>() {
	            @Override
	            public int compare(int[] o1, int[] o2) {
	                if(o1[1] != o2[1])	// b 가 같지 않은 경우 b로 오름차순 정렬
	                	return Integer.compare(o1[1], o2[1]);
	                else		// b가 같은 경우 a로 오름차순 정렬
	                	return Integer.compare(o1[0], o2[0]);
	            }
	        });
		
			
			FindMaxValue(); // 각 testcase마다 책을 줄 수 있는 학생의 최대 수를 찾는다.
		}
	}

	private static void FindMaxValue() {
		int count = 0; // 나누어주는 학생의 수를 count
		
		for(int i = 0; i < M; i++) {
			int a = book[i][0];
			int b = book[i][1];

			// 해당하는 범위 내에서
			// 가능한 가장 작은 번호의 책부터 뽑는다.
			for (int j = a-1; j < b; j++) {
				if (!check[j]) {
					check[j] = true;
					count++;
					break;
				}
			}
		}		
		
		sb.append(count + "\n");
	}
}

class InputReader {
	private final InputStream stream;
	private final byte[] buf = new byte[8192];
	private int curChar, snumChars;

	public InputReader(InputStream st) {
		this.stream = st;
	}

	public int read() {
		if (snumChars == -1)
			throw new InputMismatchException();
		if (curChar >= snumChars) {
			curChar = 0;
			try {
				snumChars = stream.read(buf);
			} catch (IOException e) {
				throw new InputMismatchException();
			}
			if (snumChars <= 0)
				return -1;
		}
		return buf[curChar++];
	}

	public int nextInt() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		int res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public long nextLong() {
		int c = read();
		while (isSpaceChar(c)) {
			c = read();
		}
		int sgn = 1;
		if (c == '-') {
			sgn = -1;
			c = read();
		}
		long res = 0;
		do {
			res *= 10;
			res += c - '0';
			c = read();
		} while (!isSpaceChar(c));
		return res * sgn;
	}

	public int[] nextIntArray(int n) {
		int a[] = new int[n];
		for (int i = 0; i < n; i++) {
			a[i] = nextInt();
		}
		return a;
	}

	public String nextLine() {
		int c = read();
		while (isSpaceChar(c))
			c = read();
		StringBuilder res = new StringBuilder();
		do {
			res.appendCodePoint(c);
			c = read();
		} while (!isEndOfLine(c));
		return res.toString();
	}

	public boolean isSpaceChar(int c) {
		return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
	}

	private boolean isEndOfLine(int c) {
		return c == '\n' || c == '\r' || c == -1;
	}
}

+ Recent posts