Skip to content

Commit

Permalink
[Gold II] Title: 철로, Time: 304 ms, Memory: 48188 KB -BaekjoonHub
Browse files Browse the repository at this point in the history
  • Loading branch information
alswo1212 committed Aug 17, 2024
1 parent 69aa43c commit 17dd8be
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
36 changes: 36 additions & 0 deletions 백준/Gold/13334. 철로/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# [Gold II] 철로 - 13334

[문제 링크](https://www.acmicpc.net/problem/13334)

### 성능 요약

메모리: 48188 KB, 시간: 304 ms

### 분류

자료 구조, 우선순위 큐, 정렬, 스위핑

### 제출 일자

2024년 8월 17일 09:38:10

### 문제 설명

<p>집과 사무실을 통근하는 n명의 사람들이 있다. 각 사람의 집과 사무실은 수평선 상에 있는 서로 다른 점에 위치하고 있다. 임의의 두 사람 A, B에 대하여, A의 집 혹은 사무실의 위치가 B의 집 혹은 사무실의 위치와 같을 수 있다. 통근을 하는 사람들의 편의를 위하여 일직선 상의 어떤 두 점을 잇는 철로를 건설하여, 기차를 운행하려고 한다. 제한된 예산 때문에, 철로의 길이는 d로 정해져 있다. 집과 사무실의 위치 모두 철로 선분에 포함되는 사람들의 수가 최대가 되도록, 철로 선분을 정하고자 한다.</p>

<p>양의 정수 d와 n 개의 정수쌍, (h<sub>i</sub>, o<sub>i</sub>), 1 ≤ i ≤ n,이 주어져 있다. 여기서 h<sub>i</sub>와 o<sub>i</sub>는 사람 i의 집과 사무실의 위치이다. 길이 d의 모든 선분 L에 대하여, 집과 사무실의 위치가 모두 L에 포함되는 사람들의 최대 수를 구하는 프로그램을 작성하시오.</p>

<p style="text-align: center;"><img alt="" src="https://onlinejudgeimages.s3-ap-northeast-1.amazonaws.com/problem/13334/1.png" style="height:140px; width:422px"></p>

<p style="text-align: center;">그림 1. 8 명의 집과 사무실의 위치</p>

<p>그림 1 에 있는 예를 고려해보자. 여기서 n = 8, (h<sub>1</sub>, o<sub>1</sub>) = (5, 40), (h<sub>2</sub>, o<sub>2</sub>) = (35, 25), (h<sub>3</sub>, o<sub>3</sub>) = (10, 20), (h<sub>4</sub>, o<sub>4</sub>) = (10, 25), (h<sub>5</sub>, o<sub>5</sub>) = (30, 50), (h<sub>6</sub>, o<sub>6</sub>) = (50, 60), (h<sub>7</sub>, o<sub>7</sub>) = (30, 25), (h<sub>8</sub>, o<sub>8</sub>) = (80, 100)이고, d = 30이다. 이 예에서, 위치 10 과 40 사이의 빨간색 선분 L이, 가장 많은 사람들에 대하여 집과 사무실 위치 모두 포함되는 선분 중 하나이다. 따라서 답은 4 이다.</p>

### 입력

<p>입력은 표준입력을 사용한다. 첫 번째 줄에 사람 수를 나타내는 양의 정수 n (1 ≤ n ≤ 100,000)이 주어진다. 다음 n개의 각 줄에 정수 쌍 (h<sub>i</sub>, o<sub>i</sub>)가 주어진다. 여기서 h<sub>i</sub>와 o<sub>i</sub>는 −100,000,000이상, 100,000,000이하의 서로 다른 정수이다. 마지막 줄에, 철로의 길이를 나타내는 정수 d (1 ≤ d ≤ 200,000,000)가 주어진다.</p>

### 출력

<p>출력은 표준출력을 사용한다. 길이 d의 임의의 선분에 대하여, 집과 사무실 위치가 모두 그 선분에 포함되는 사람들의 최대 수를 한 줄에 출력한다. </p>

22 changes: 22 additions & 0 deletions 백준/Gold/13334. 철로/철로.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import sys
from heapq import heappop, heappush
n = int(sys.stdin.readline())
lines = []
for _ in range(n):
a,b = map(int,sys.stdin.readline().split())
lines.append((min(a,b), max(a,b)))
lines.sort(key=lambda l : l[1])

L = int(sys.stdin.readline())
heap = []

max_cnt = 0
for line in lines:
if line[1] - line[0] > L: continue

while heap and heap[0] < line[1] - L:
heappop(heap)

heappush(heap, line[0])
max_cnt = max(max_cnt, len(heap))
print(max_cnt)

0 comments on commit 17dd8be

Please sign in to comment.