[백준] 4307번 개미 (java, 구현)

728x90
반응형

www.acmicpc.net/problem/4307

 

4307번: 개미

개미 여러 마리가 길이가 lcm인 막대 위에 있다. 각 개미의 이동 속도는 모두 일정하며, 1cm/s이다. 개미가 막대의 마지막까지 걸어간다면, 개미는 그 즉시 떨어지게 된다. 또, 두 개미가 만나게 된

www.acmicpc.net

최소 시간은 각 개미들이 떨어지는 시간의 최대값을 구해주면 된다.

 

최대값은 각 개미들의 양 끝 거리 중 더 큰 값의 최대값을 구해주면 된다.

 

package algo0421;

import java.util.ArrayList;
import java.util.Scanner;

public class S_4307_개미_Main {
	static int t,l,n;
	static ArrayList<Integer> list;
	public static void main(String[] args) {
		
		Scanner sc=new Scanner(System.in);
		t=sc.nextInt();
		for (int tc = 0; tc < t; tc++) {
			l=sc.nextInt();
			n=sc.nextInt();
			list=new ArrayList<>();
			for (int i = 0; i < n; i++) {
				list.add(sc.nextInt());//각 개미 위치 
			}
			int min=0;
			for (int i = 0; i < n; i++) {
				int temp=Math.min(list.get(i), l-list.get(i));
				min=Math.max(min, temp);
			}
			
			int max=0;
			for (int i = 0; i < n; i++) {
				max=Math.max(max, list.get(i));
				max=Math.max(max, l-list.get(i));
			}
			System.out.println(min+" "+max);
		}
	}
}
728x90
반응형
TAGS.

Comments