반응형
 

13334번: 철로

입력은 표준입력을 사용한다. 첫 번째 줄에 사람 수를 나타내는 양의 정수 n (1 ≤ n ≤ 100,000)이 주어진다. 다음 n개의 각 줄에 정수 쌍 (hi, oi)가 주어진다. 여기서 hi와 oi는 −100,000,000이상, 100,000,0

www.acmicpc.net

 

집과 사무실의 위치를 하나의 선분으로 봤을 때, 도착 지점을 -d만큼 이동시켜 준 뒤, 겹치는 선분의 개수를 구해주었습니다.

 

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int n, h, o, d, ans = 0;
pair<int, int> ho[100000];
vector<pair<int, int>> arr;

bool compare(pair<int, int>& a, pair<int, int>& b) {
	if (a.first == b.first) return a.second > b.second;
	else return a.first < b.first;
}

int main() {
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d %d", &h, &o);
		if (h > o) ho[i] = { o, h };
		else ho[i] = { h, o };
	}
	scanf("%d", &d);
	for (int i = 0; i < n; i++) {
		if (ho[i].second - ho[i].first <= d) {
			arr.push_back({ ho[i].first, -1 });
			arr.push_back({ ho[i].second - d, 1 });
		}
	}
	sort(arr.begin(), arr.end(), compare);
	int cnt = 0;
	for (int i = 0; i < arr.size(); i++) {
		cnt += arr[i].second;
		ans = max(ans, cnt);
	}
	printf("%d", ans);
}
반응형

'Algorithm' 카테고리의 다른 글

백준 21924 : 도시 건설  (0) 2021.11.17
백준 2141, 2285 : 우체국  (0) 2021.11.17
백준 6497 : 전력난  (0) 2021.11.17
백준 15926 : 현욱은 괄호왕이야!!  (0) 2021.11.17
백준 5214 : 환승  (0) 2021.11.17

+ Recent posts