Skip to content

Commit daccbce

Browse files
authored
Merge pull request #40 from ChanhuiSeok/ps
철로
2 parents 2f36997 + 9dd232e commit daccbce

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <string>
5+
#include <queue>
6+
7+
using namespace std;
8+
// 블로그에 포스팅해서 정리할 만한 문제인 것 같다.
9+
// 참고로 라인 스위핑은 priority_queue를 많이 사용한다고 한다.
10+
11+
#define pii pair<int, int>
12+
13+
vector<pair<int, int>> input;
14+
15+
int n, d;
16+
17+
struct comp {
18+
bool operator()(const pair<int, int>& a, const pair<int, int>& b) {
19+
/* priority_queue는 comp를 정의 시, 일반적인 sort에서 comp함수 정의때와
20+
정반대로 쓰면 된다. 오름차순을 아래처럼 작성했다. */
21+
if (a.second == b.second) {
22+
return a.first > b.first;
23+
}
24+
return a.second > b.second;
25+
}
26+
};
27+
28+
priority_queue<pii, vector<pii>, comp> inputQ;
29+
priority_queue<int, vector<int>, greater<int>> pq;
30+
31+
bool compare(const pair<int, int>& a, const pair<int, int>& b) {
32+
if (a.second != b.second) {
33+
return a.second < b.second;
34+
}
35+
else if (a.second == b.second) {
36+
return a.first < b.first;
37+
}
38+
}
39+
40+
int main() {
41+
42+
ios::sync_with_stdio(0);
43+
cin.tie(0);
44+
cout.tie(0);
45+
46+
cin >> n;
47+
for (int i = 0; i < n; i++) {
48+
int h, o;
49+
cin >> h >> o;
50+
if (h > o)inputQ.push({ o,h });
51+
else inputQ.push({ h,o });
52+
}
53+
cin >> d;
54+
55+
int maxSize = 0;
56+
57+
// n개만큼 살펴본다.
58+
while (!inputQ.empty()) {
59+
int iR = inputQ.top().second;
60+
int iL = inputQ.top().first;
61+
inputQ.pop();
62+
63+
// iR-iL 사이즈가 d보다 작으면 일단 가능한 후보니까 그 왼쪽 끝점을 넣는다.
64+
if (iR - iL <= d) {
65+
pq.push(iL);
66+
}
67+
68+
while (!pq.empty()) {
69+
// 가능한 왼쪽 시작점 후보들이 있는 pq 에서 하나를 뽑아서
70+
// 현재 살펴보고 있는 n번째 인풋의 오른쪽 끝값과의 거리를 계산하고
71+
// 그 거리가 d 이하이면 나가서 다른 인풋을 계속 보며
72+
// d 보다 큰 거리이면 가능한 왼쪽 시작점 후보에서 그것을 빼버린다.
73+
int tmp = pq.top();
74+
if (iR - tmp <= d) break;
75+
else
76+
pq.pop();
77+
}
78+
maxSize = max(maxSize, (int)pq.size());
79+
}
80+
81+
cout << maxSize;
82+
83+
return 0;
84+
}

0 commit comments

Comments
 (0)