-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbank.c
75 lines (57 loc) · 1.17 KB
/
bank.c
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
#include "stdlib.h"
#include "stdio.h"
int N;
int T;
typedef struct person {
long c;
int t;
} person;
person P[10000]; // N <= 10000
// dictionary order
int t_cmp(const void* ap, const void* bp) {
person a = *(person*) ap;
person b = *(person*) bp;
if (a.t > b.t)
return 1;
if (a.t < b.t)
return -1;
return 0;
}
void production() {
scanf("%d %d", &N, &T);
for (int i=0; i<N; i++)
scanf("%ld %d", &P[i].c, &P[i].t);
qsort(P, N, sizeof(person), t_cmp);
long long sum = 0;
int top = N;
// goin in reverse
for (int t=T; t>=0; t--) {
int bottom = 0;
// find all people who are still here
for (; bottom<top && P[bottom].t < t; bottom++);
if (bottom == top) continue;
// take max of those who are here
long max = -1;
int maxi = -1;
for (int i=bottom; i<top; i++)
if (P[i].c > max) {
max = P[i].c;
maxi = i;
}
sum += max;
top--;
// swap and bubble up - maintain sortedness
P[maxi] = P[top];
qsort(P, top, sizeof(person), t_cmp);
// for (int i=maxi; i<top-1 && P[i].t < P[i+1].t; i++) {
// person tmp = P[i+1];
// P[i+1] = P[i];
// P[i] = tmp;
// }
}
printf("%lld\n", sum);
}
int main() {
production();
return 0;
}