-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSPOJ-EXPEDI.cpp
More file actions
109 lines (86 loc) · 1.46 KB
/
SPOJ-EXPEDI.cpp
File metadata and controls
109 lines (86 loc) · 1.46 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> stop[10001];
int heapsize = 0;
void bubble_up(int h[]){
int i = heapsize-1;
int j;
if(i % 2 == 0) j = i/2-1;
else j = i/2;
while(i > 0 && h[i] >= h[j]){
int aux;
aux = h[i];
h[i] = h[j];
h[j] = aux;
i = j;
if(i % 2 == 0) j = i/2-1;
else j = i/2;
}
}
void heap_insert(int h[], int k){
h[heapsize] = k;
heapsize++;
bubble_up(h);
}
void heapify(int h[], int i){
int m, l, r;
m = i;
l = 2*i + 1;
r = 2*i + 2;
if(l < heapsize && h[l] >= h[m]){
m = l;
}
if(r < heapsize && h[r] >= h[m]){
m = r;
}
if(m != i){
int aux;
aux = h[i];
h[i] = h[m];
h[m] = aux;
heapify(h, m);
}
}
int heap_extract(int h[]){
int ret = h[0];
swap(h[0], h[heapsize-1]);
heapsize--;
heapify(h, 0);
return ret;
}
int main() {
int t;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
heapsize = 0;
int P, L;
int d, c;
int maxHeap[10001];
for (int i = 0; i < n; ++i) {
scanf("%d %d", &d, &c);
stop[i] = {d, c};
}
scanf("%d %d", &d, &c);
for (int i = 0; i < n; ++i){
stop[i].first = d - stop[i].first;
}
int cnt = 0, MAX = 0;
sort(stop, stop + n);
stop[n] = {d, 0};
for (int i = 0; i < n + 1; ++i) {
while (c < stop[i].first && !(heapsize == 0)) {
c += heap_extract(maxHeap);
++cnt;
}
if (c < stop[i].first)
break;
heap_insert(maxHeap, stop[i].second);
}
if (c < d)
cnt = -1;
printf("%d\n", cnt);
}
return 0;
}