-
Notifications
You must be signed in to change notification settings - Fork 0
/
d_clustering.cpp
227 lines (189 loc) · 4.53 KB
/
d_clustering.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
long long rdtsc() {
long long tmp;
asm("rdtsc" : "=A"(tmp));
return tmp;
}
inline int myrand() {
#ifdef _WIN32
return abs((rand() << 15) ^ rand());
#else
return rand();
#endif
}
inline int rnd(int x) {
return myrand() % x;
}
#ifdef LOCAL
#define LLD "%lld"
#else
#ifdef _WIN32
#define LLD "%I64d"
#else
#define LLD "%lld"
#endif
#endif
#ifdef DEBUG
#define eprintf(...) fprintf(stderr, __VA_ARGS__), fflush(stdout)
#else
#define eprintf(...) ;
#endif
#define pb push_back
#define mp make_pair
#define sz(x) ((int)(x).size())
#define TASK "text"
void precalc() {
}
struct Pnt {
int x, y;
Pnt(): x(), y() {}
Pnt(int x, int y): x(x), y(y) {}
int dist(const Pnt & p) const {
return abs(x - p.x) + abs(y - p.y);
}
};
struct Ride {
Pnt s, f;
int ts, tf;
int num;
void read() {
scanf("%d%d%d%d%d%d", &s.x, &s.y, &f.x, &f.y, &ts, &tf);
}
bool operator <(const Ride & r) const {
return ts < r.ts;
}
};
bool is_good(const Pnt &p) {
return p.x < 9000 && p.y < 9000;
}
const int maxn = 10010;
const int maxx = 10010;
const int maxf = 1010;
int r, c, f, n, b, t;
Ride rides[maxn];
int bn, bf;
Ride brides[maxn];
bool read() {
if (scanf("%d%d%d%d%d%d", &r, &c, &f, &n, &b, &t) < 6) {
return false;
}
for (int i = 0; i < n; ++i) {
rides[i].read();
rides[i].num = i;
}
bn = 0;
for (int i = 0; i < n; i++) {
if (!is_good(rides[i].s) || !is_good(rides[i].f)) {
swap(rides[i], rides[n - 1]);
brides[bn++] = rides[n - 1];
n--;
i--;
}
}
bf = 62;
f -= bf;
return true;
}
vector<int> ans[maxf];
vector<int> score[maxf];
vector<Pnt> pos[maxf];
vector<int> tfree[maxf];
void solve() {
for (int i = 0; i < f; ++i) {
ans[i].clear();
score[i].clear();
pos[i].clear();
tfree[i].clear();
pos[i].pb(Pnt(0, 0));
tfree[i].pb(0);
}
sort(rides, rides + n);
for (int i = 0; i < n; ++i) {
int ridelen = rides[i].s.dist(rides[i].f);
int chosen = -1, chtget = -1;
for (int j = 0; j < f; ++j) {
int ctfree = *tfree[j].rbegin();
Pnt cpos = *pos[j].rbegin();
int tget = max(rides[i].ts, ctfree + cpos.dist(rides[i].s));
if (tget + ridelen <= rides[i].tf) {
if (chosen == -1 || tget < chtget || (tget == chtget && ctfree > (*tfree[chosen].rbegin()))) {
chosen = j;
chtget = tget;
}
}
}
if (chosen != -1) {
ans[chosen].pb(rides[i].num);
score[chosen].pb(ridelen + (chtget == rides[i].ts ? b : 0));
tfree[chosen].pb(chtget + ridelen);
pos[chosen].pb(rides[i].f);
} else {
int chosen = -1, chtget = -1, chk = -1, chdiff = 0;
for (int j = 0; j < f; ++j) {
int diff = 0;
for (int k = sz(tfree[j]) - 2; k >= 0; --k) {
diff -= score[j][k];
int ctfree = tfree[j][k];
Pnt cpos = pos[j][k];
int tget = max(rides[i].ts, ctfree + cpos.dist(rides[i].s));
if (tget + ridelen <= rides[i].tf) {
int cscore = ridelen + (tget == rides[i].ts ? b : 0);
if (cscore + diff > chdiff) {
chosen = j;
chk = k;
chtget = tget;
chdiff = diff + cscore;
}
break;
}
}
}
if (chosen != -1) {
while (sz(tfree[chosen]) - 1 > chk) {
tfree[chosen].pop_back();
pos[chosen].pop_back();
score[chosen].pop_back();
ans[chosen].pop_back();
}
ans[chosen].pb(rides[i].num);
score[chosen].pb(ridelen + (chtget == rides[i].ts ? b : 0));
tfree[chosen].pb(chtget + ridelen);
pos[chosen].pb(rides[i].f);
}
}
}
int sum = 0;
for (int i = 0; i < f; ++i) {
printf("%d", sz(ans[i]));
for (int j = 0; j < sz(ans[i]); ++j) {
sum += score[i][j];
printf(" %d", ans[i][j]);
}
printf("\n");
}
eprintf("%d\n", sum);
}
int main() {
srand(rdtsc());
precalc();
while (true) {
if (!read()) {
break;
}
solve();
for (int i = 0; i < bn; i++) {
rides[i] = brides[i];
}
n = bn;
f = bf;
solve();
#ifdef DEBUG
eprintf("Time %.2f\n", (double) clock() / CLOCKS_PER_SEC);
#endif
return 0;
}
return 0;
}