-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtrenuri.cpp
126 lines (108 loc) · 3.08 KB
/
trenuri.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# include <algorithm>
# include <fstream>
# include <cstring>
# include <vector>
# include <set>
# define mp make_pair
# define NR 100005
using namespace std;
struct elem {
int dest, i, t;
}E,E2;
struct comp {
bool operator () (const elem &x, const elem &y) {
if (x.dest <= y.dest) return 0;
else return 1;
}
};
struct comp2 {
bool operator () (const elem &x, const elem &y) {
if (x.dest >= y.dest) return 0;
else return 1;
}
};
struct elem2 {
int dest, aux, i;
}tren[NR], om[NR];
int i,j,n,m,nrT,capacitate,destinatie,inceput,sfarsit,H;
int FREE,Free,nrsol,VV,T,maxx,OM;
int locuri[NR], sol[NR];
bool cmp (elem2 x, elem2 y) {
if (x.dest <= y.dest) return 0;
else return 1;
}
int main ()
{
freopen ("trenuri3.in", "r", stdin);
freopen ("trenuri3.out", "w", stdout);
scanf ("%d%d", &n, &m);
for (i=1; i<=n; ++i) {
scanf ("%d%d", &destinatie, &capacitate);
maxx=max(maxx, destinatie);
tren[i].dest=destinatie; tren[i].aux=capacitate;
tren[i].i=i;
}
// sortam descrescator dupa statie de destinatie
sort (tren+1, tren+n+1, cmp);
for (i=1; i<=m; ++i) {
scanf ("%d%d", &inceput, &sfarsit);
om[i].dest=sfarsit; om[i].aux=inceput;
om[i].i=i;
}
// sortam descrescator dupa sfarsit
sort (om+1, om+m+1, cmp);
while (om[OM+1].dest > maxx) ++OM;
multiset <elem, comp> SET;
multiset <elem, comp2> SET2;
// VV-nivelul in stiva cu locuri goale
for (i=maxx; i>=1; --i) {
//incepe un nou tren
while (tren[T+1].dest==i) {
++T;
// adaugam numarul de locuri ale acestui tren
while (FREE < m && tren[T].aux) {
++FREE;
locuri[++Free]=tren[T].i;
--tren[T].aux;
}
}
//vedem cine iese acum
while (SET.size() && (*SET.begin()).dest==i) {
E=*SET.begin();
SET.erase(SET.find(E));
SET2.erase (SET2.find(E));
++nrsol; sol[E.i]=E.t;
locuri[++Free]=E.t;
}
//punem oamenii in set
while (om[OM+1].dest==i) {
++OM;
//daca avem locuri libere
if (Free) {
E.dest=om[OM].aux;
E.i=om[OM].i;
E.t=locuri[Free];
--Free;
SET.insert (E);
SET2.insert (E);
}
else {
//vedem daca sunt trenuri care ies dupa el
//-> suntem greedy si il inlocuim cu asta
if (SET2.size() && (*SET2.begin()).dest < om[OM].aux) {
E=*SET2.begin();
SET.erase(SET.find(E));
SET2.erase(SET2.find(E));
E.i=om[OM].i;
E.dest=om[OM].aux;
SET.insert(E);
SET2.insert(E);
}
}
}
}
printf ("%d\n", nrsol);
for (i=1; i<=m; ++i)
printf ("%d\n", sol[i]);
return 0;
}