Java

[๋ฐฑ์ค€ 20440] ๐ŸŽต๋‹ˆ๊ฐ€ ์‹ซ์–ด ์‹ซ์–ด ๋„ˆ๋ฌด ์‹ซ์–ด ์‹ซ์–ด ์˜ค์ง€ ๋งˆ ๋‚ด๊ฒŒ ์ฐ์ฉ๋Œ€์ง€๋งˆ๐ŸŽต - 1 (JAVA)

iheeeee6-6 2023. 2. 21. 12:00
728x90

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

 

20440๋ฒˆ: ๐ŸŽต๋‹ˆ๊ฐ€ ์‹ซ์–ด ์‹ซ์–ด ๋„ˆ๋ฌด ์‹ซ์–ด ์‹ซ์–ด ์˜ค์ง€ ๋งˆ ๋‚ด๊ฒŒ ์ฐ์ฉ๋Œ€์ง€๋งˆ๐ŸŽต - 1

์ฒซ์งธ ์ค„์— ์ง€๋™์ด์˜ ๋ฐฉ์— ์ถœ์ž…ํ•œ ๋ชจ๊ธฐ์˜ ๋งˆ๋ฆฟ์ˆ˜ N(1 ≤ N ≤ 1,000,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ N๊ฐœ์˜ ์ค„์— ๋ชจ๊ธฐ์˜ ์ž…์žฅ ์‹œ๊ฐ TE๊ณผ ํ‡ด์žฅ ์‹œ๊ฐ TX์ด ์ฃผ์–ด์ง„๋‹ค. (0 ≤ TE < TX ≤ 2,100,000,000) ๋ชจ๊ธฐ๋Š” [TE, Tx)๋™

www.acmicpc.net

 

Map์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

๋ชจ๊ธฐ๊ฐ€ ๋“ค์–ด์˜ฌ๋•Œ +1, ๋‚˜๊ฐˆ๋•Œ -1์„ ํ•˜์—ฌ

๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ฐ€์ง„ ์‹œ๊ฐ„์ด ๋ชจ๊ธฐ๊ฐ€ ๊ฐ€์žฅ ๋งŽ์€ ์‹œ๊ฐ„์ธ ๊ฒƒ์ด๋‹ค.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

public class Main {
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());
		Map<Integer,Integer> map = new HashMap<>();
		Map<Integer,Integer> map2 = new HashMap<>();
		StringTokenizer st;
		for(int i=0;i<n;i++) {
			st = new StringTokenizer(br.readLine());
			int start = Integer.parseInt(st.nextToken());
			int end = Integer.parseInt(st.nextToken());
			map.put(start, map.get(start)!=null? map.get(start)+1:1);
			map.put(end, map.get(end)!=null? map.get(end)-1:-1);
		}
		
		ArrayList<Integer> arrlist =new ArrayList<>(map.keySet());
		Collections.sort(arrlist);
		
		int count=0;
		int maxCount=0;
		int maxstart=0;
		int maxend=0;
		int maxstartidx=0;
		for(int i=0;i<arrlist.size();i++) {
			int temp =count+map.get(arrlist.get(i));
			count=temp;
			map2.put(arrlist.get(i), temp);
			if(maxCount<temp) {
				maxCount=temp;
				maxstart=arrlist.get(i);
				maxstartidx=i;
			}
		}
		
		while(true) {
			if(map2.get(arrlist.get(++maxstartidx))!=maxCount)
			{
				maxend=	arrlist.get(maxstartidx);
				break;
			}
		}
		System.out.println(maxCount);
		System.out.println(maxstart+" "+maxend);
	}

}