-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1826.cpp
More file actions
77 lines (67 loc) · 1.34 KB
/
1826.cpp
File metadata and controls
77 lines (67 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// 1826. 연료 채우기
// 2020.06.14
// 그리디 알고리즘
#include<iostream>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
priority_queue<int> pq;
struct gasStation
{
int distance;
int amount;
};
bool compare(gasStation& a, gasStation& b)
{
return a.distance < b.distance;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int l, q, n;
int idx = 0;
int ans = 0;
cin >> n;
vector<gasStation> gasStations(n); // 거리, 주유량
for (int i = 0; i < n; i++)
{
cin >> gasStations[i].distance >> gasStations[i].amount;
}
cin >> l >> q;
// 거리 기준 정렬
sort(gasStations.begin(), gasStations.end(), compare);
while (q < l)
{
// 현재 갈 수 있는 주유소들의 주유량을 힙에 넣음
while (gasStations[idx].distance <= q)
{
pq.push(gasStations[idx].amount);
idx++;
if (idx == n)
{
break;
}
}
if (pq.empty())
{
break;
}
// 기름 추가
q += pq.top();
pq.pop();
ans++;
}
// 도착 못했을 경우
if (q < l)
{
cout << -1 << endl;
}
// 도착 완료
else
{
cout << ans << endl;
}
return 0;
}