๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”Ž Algorithm/๐Ÿ“ ๊ฐœ๋… ์ •๋ฆฌ

[๊ฐœ๋…์ •๋ฆฌ] ์Šค์œ„ํ•‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜

by Seongpyo Hong 2021. 2. 26.

์Šค์œ„ํ•‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด๋ž€ ํŠน์ • ๊ธฐ์ค€์— ๋”ฐ๋ผ ์ •๋ ฌ๋œ ์ˆœ์„œ๋Œ€๋กœ ๋ฌธ์ œ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์ด๋‹ค. ์Šค์œ„ํ•‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์•ผํ•˜๋Š” ๋ฌธ์ œ๋“ค์˜ ํŠน์ง•์€ ๋Ÿฌํ”„ํ•œ ๋ฐฉ๋ฒ• (์ผ๋ฐ˜์ ์œผ๋กœ O(N^2)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ–๋Š” ๋ฐฉ๋ฒ•)์œผ๋กœ๋Š” ํ•ด๊ฒฐ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋ฉฐ, DP๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ์—๋Š” ๋ฉ”๋ชจ์ด์ œ์ด์…˜ ํ•ด์•ผํ•  ์ˆ˜๊ฐ€ ๋„ˆ๋ฌด ๋งŽ์€ ๊ฒฝ์šฐ๊ฐ€ ์ผ๋ฐ˜์ ์ด๋‹ค. ์Šค์œ„ํ•‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ๋ฌธ์ œ๋ฅผ ํ’€ ๊ฒฝ์šฐ, ํ•ด๋‹น ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ์ •๋ ฌ์—์„œ ๋ฐœ์ƒํ•˜๋Š” O(NlogN)์•ˆ์—์„œ ํ•ด๊ฒฐ๋œ๋‹ค.


๋ฐฑ์ค€ - 8982. ์‚ฌ๋ƒฅ๊พผ

์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์œผ๋กœ ํ’€๊ฒŒ๋˜๋ฉด O(N*M)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋˜๊ณ  ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๊ทธ๋ ‡๋‹ค๊ณ  ์ขŒํ‘œ์— ๋Œ€ํ•œ ์ •๋ณด๋ฅผ ๊ธฐ์–ตํ•  ์ˆ˜๋„ ์—†๊ธฐ ๋•Œ๋ฌธ์— ์Šค์œ„ํ•‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ํ†ตํ•ด ํ•ด๊ฒฐํ•ด์•ผ ํ•œ๋‹ค. 

๋จผ์ €, ์‚ฌ๋Œ€์™€ ๋™๋ฌผ์„ ๋ชจ๋‘ x์ถ• ์ˆœ์„œ๋กœ ์ •๋ ฌํ•˜๊ณ  ๋™๋ฌผ์„ ๊ธฐ์ค€์œผ๋กœ ์‚ฌ๋ƒฅ์ด ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•œ๋‹ค. ํ™•์ธ ๊ณผ์ •์€ ๋™๋ฌผ์˜ x์ขŒํ‘œ๋ณด๋‹ค ์ž‘์€ ์‚ฌ๋Œ€๋ฅผ ์ฐพ๊ณ  ํ•ด๋‹น ์‚ฌ๋Œ€์™€ ๋ฐ”๋กœ ๋‹ค์Œ ์‚ฌ๋Œ€์—์„œ ๊ฑฐ๋ฆฌ๊ฐ€ L ์ดํ•˜๋ฉด ์‚ฌ๋ƒฅ์ด ๊ฐ€๋Šฅํ•œ ๊ฒƒ์œผ๋กœ ํŒ๋ณ„ํ•œ๋‹ค.

ํ’€์ด

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;

public class Solution {
	static class Pair implements Comparable<Pair>{
		int x;
		int y;
		public Pair(int x, int y) {
			this.x = x;
			this.y = y;
		}
		@Override
		public int compareTo(Pair o) {
			if (this.x > o.x) {
				return 1;
			} else if (this.x == o.x && this.y > o.x) {
				return 1;
			}
			return -1;
		}
	}
	
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static int M, N, L, startIdx = 0, cnt;
	static int[] shoot;
	static Pair[] animal;
	public static void main(String[] args) throws IOException {
		String[] input = br.readLine().split(" ");
		M = Integer.parseInt(input[0]);
		N = Integer.parseInt(input[1]);
		L = Integer.parseInt(input[2]);
		
		shoot = new int[M];
		animal = new Pair[N];
		String[] shootLine = br.readLine().split(" ");
		for (int i = 0; i < M; i++) {
			shoot[i] = Integer.parseInt(shootLine[i]);
		}
		
		for (int i = 0; i < N; i++) {
			String[] line = br.readLine().split(" ");
			animal[i] = new Pair(Integer.parseInt(line[0]), Integer.parseInt(line[1]));
		}
		
		Arrays.sort(shoot);
		Arrays.sort(animal);

		for (int i = 0; i < N; i++) {
			shoot(i);
		}

		System.out.println(cnt);
	}
	
	static void shoot(int idx) {
		int shootIdx = 0;
		while(shootIdx != M-1 && shoot[shootIdx] <= animal[idx].x) {
			shootIdx++;
		}
		
		if (--shootIdx >= 0 && Math.abs(shoot[shootIdx] - animal[idx].x) + animal[idx].y <= L) {
			cnt +=1;
		} else if(++shootIdx < M && Math.abs(shoot[shootIdx] - animal[idx].x) + animal[idx].y <= L) {
			cnt+= 1;
		}
		return;
	}
}

 


๋ฐฑ์ค€ - 2170. ์„ ๊ธ‹๊ธฐ

์ด ๋ฌธ์ œ๋„ ์—ญ์‹œ ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ•˜๋ฉด ์‹œ๊ฐ„ ์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๊ณ , ์ขŒํ‘œ์— ๋Œ€ํ•œ ์„  ์—ฌ๋ถ€๋ฅผ ์ €์žฅํ•˜๊ธฐ์—๋Š” ๋‘ ์ ์˜ ์œ„์น˜ ๋ฒ”์œ„๊ฐ€ ๋„ˆ๋ฌด ํฌ๋‹ค. ๋”ฐ๋ผ์„œ ์Šค์œ„ํ•‘ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์‚ฌ์šฉํ•ด์„œ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

๋จผ์ € x๋ฅผ ๊ธฐ์ค€์œผ๋กœ x๊ฐ€ ๊ฐ™๋‹ค๋ฉด y๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค. ์ดํ›„ ํ˜„์žฌ end๊ฐ€ ๋‹ค์Œ ์Œ์˜ start๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ์—๋Š” ๊ฒน์น˜๋Š” ๊ตฌ๊ฐ„์ด ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— end ๊ฐ’์„ ํฐ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค. ํ˜„์žฌ end๊ฐ€ ๋‹ค์Œ ์Œ์˜ start๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ์—๋Š” ๊ฒน์น˜์ง€ ์•Š๋Š” ์ƒˆ๋กœ์šด ์„ ๋ถ„์˜ ์‹œ์ž‘์œผ๋กœ ๋ณด๊ณ  ์ด์ „ ์„ ๋ถ„์˜ ๊ธธ์ด (end - start)๋ฅผ ๋”ํ•ด์ฃผ๊ณ  start, end๋ฅผ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค.

ํ’€์ด

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

public class Solution1 {
	static class Pair implements Comparable<Pair>{
		int start;
		int end;
		public Pair(int start, int end) {
			this.start = start;
			this.end = end;
		}
		@Override
		public int compareTo(Pair o) {
			if (this.start > o.start) {
				return 1;
			} else if (this.start == o.start && this.end > o.end) {
				return 1;
			}
			return -1;
		}
		
	}
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static int N;
	static Pair[] points;
	public static void main(String[] args) throws NumberFormatException, IOException {
		N = Integer.parseInt(br.readLine());
		points = new Pair[N];
		for (int i = 0; i < N; i++) {
			String[] line = br.readLine().split(" ");
			points[i] = new Pair(Integer.parseInt(line[0]), Integer.parseInt(line[1]));
		}
		
		Arrays.sort(points);
		
		int curIdx = 0;
		int distance = 0;
		while(curIdx < N) {
			int start = points[curIdx].start;
			int end = points[curIdx].end;
			while(++curIdx < N) {
				if (points[curIdx].start <= end) 
					end = Math.max(end, points[curIdx].end);
				else 
					break;
			}
			distance += (end - start);
		}
		
		System.out.println(distance);
	}
}

๋Œ“๊ธ€