Coding Test/baekjoon

[백준 1263] 시간 관리- java (solved.ac - 실버 1)

조용장 2022. 9. 14. 23:40

https://www.acmicpc.net/problem/1263

 

1263번: 시간 관리

진영이는 캠프 조교를 온 후 효율적으로 시간 관리를 해야 한다는 것을 깨달았다. 진영이는 하루에 해야 할 일이 총 N개가 있고 이 일들을 편하게 1번부터 N번까지 차례대로 번호를 붙였다. 진영

www.acmicpc.net

풀이

문제를 보았을 때 알 수 있는 힌트

문제를 보면 알 수 있는 부분으로 모든 일을 처리해야며 최대한 늦게 일을 시작하게 만들면 되는 문제이다.

최소 쪽을 정렬하던 최대쪽을 정렬하면 될것같은 문제인데 나는 S를 최대로 하여 정렬하였다. 

S의 시안에는 일을 처리해야하기 때문에 이것을 기준으로 잡았다.

S의 시에서 T시간을 뺀 값(result)을 다음에 할일을 할 수 있는 시간이 되는 것이다.

다음으로 할 시간을 보았을때 뺀 값(result)보다 S의 시간이 적은 경우 뺀값은 S의 시로 변경한다.

이를 반복하여 일이 끝날때까지 돌려보면 마지막에 늦게 시작할수 있는 시간이 나온다.

이때 늦게 시작하는 시간이 음수가 나오면 일을 끝마칠수 없는 것으로 -1이 출력 되게 하면 된다.

 

package Baekjoon1263;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
	static int n;
	static Time time[];
	static class Time implements Comparable<Time>{
		int t;
		int s;
		public Time(int t, int s) {
			super();
			this.t = t;
			this.s = s;
		}
		@Override
		public int compareTo(Time o) {
			// TODO Auto-generated method stub
			if(o.s == this.s) {
				return Integer.compare(o.t, this.t);
			}
			return Integer.compare(o.s,this.s);
		}
		
		
	}
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer token;
		n = Integer.parseInt(br.readLine());
		time = new Time[n];
		for (int i = 0; i < n; i++) {
			token = new StringTokenizer(br.readLine());
			int t = Integer.parseInt(token.nextToken());
			int s = Integer.parseInt(token.nextToken());
			time[i]= new Time(t, s);
		}
		Arrays.sort(time);

		int result = time[0].s;
		for (int i = 0; i < n; i++) {
			if(time[i].s<result) result=time[i].s;
			result -= time[i].t;
		}
		if(result<0) {
			System.out.println(-1);
		}
		else {
			System.out.println(result);
		}
	}

}

https://github.com/dydwkd486/coding_test/blob/main/baekjoon/java/Baekjoon1263/Main.java

 

GitHub - dydwkd486/coding_test: 코딩테스트 공부한 내용 정리

코딩테스트 공부한 내용 정리. Contribute to dydwkd486/coding_test development by creating an account on GitHub.

github.com