-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathgoogle_kickstart_G.cpp
377 lines (343 loc) · 11.2 KB
/
google_kickstart_G.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define YES cout << "YES\n"
#define NO cout << "NO\n"
#define pb push_back
// #define endl "\n"
typedef long long ll;
#define forp(i, x, t) for (int i = x; i < t; i++)
#define forn(i, x, t) for (int i = x; i > t; i--)
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
const int mod = 1e9 + 7;
const int N = 2e5 + 5;
const int MAXN = 10000001;
int spf[MAXN];
int fact[N], invfact[N];
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
#define int long long
// void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define deb(x...)
#endif
/*
AUTHOR:
$$$$$$$\ $$\
$$ __$$\ $$ |
$$ | $$ | $$$$$$\ $$$$$$$\ $$$$$$\ $$$$$$$\
$$$$$$$ |$$ __$$\ $$ __$$\ \____$$\ $$ __$$\
$$ __$$< $$ / $$ |$$ | $$ | $$$$$$$ |$$ | $$ |
$$ | $$ |$$ | $$ |$$ | $$ |$$ __$$ |$$ | $$ |
$$ | $$ |\$$$$$$ |$$ | $$ |\$$$$$$$ |$$ | $$ |
\__| \__| \______/ \__| \__| \_______|\__| \__|
$$$$$$$\ $$\ $$\ $$\
$$ __$$\ \__|$$ | $$ |
$$ | $$ |$$$$$$\ $$$$$$\ $$\ $$ | $$\ $$$$$$$\
$$$$$$$ |\____$$\ $$ __$$\ $$ |$$ | $$ |$$ __$$\
$$ ____/ $$$$$$$ |$$ | \__|$$ |$$$$$$ / $$ | $$ |
$$ | $$ __$$ |$$ | $$ |$$ _$$< $$ | $$ |
$$ | \$$$$$$$ |$$ | $$ |$$ | \$$\ $$ | $$ |
\__| \_______|\__| \__|\__| \__|\__| \__|
*/
struct custom_hash {
static uint64_t splitmix64(uint64_t x) {
// http://xorshift.di.unimi.it/splitmix64.c
x += 0x9e3779b97f4a7c15;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
return x ^ (x >> 31);
}
size_t operator()(uint64_t x) const {
static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
return splitmix64(x + FIXED_RANDOM);
}
};
bool is_prime(ll n)
{
if (n == 1)
{
return false;
}
int i = 2;
while (i * i <= n)
{
if (n % i == 0)
{
return false;
}
i += 1;
}
return true;
}
int pow(int a, int b, int m)
{
int ans=1;
while(b)
{
if(b&1)
ans=(ans*a)%m;
b/=2;
a=(a*a)%m;
}
return ans;
}
int modinv(int k)
{
return pow(k, mod-2, mod);
}
void precompute()
{
fact[0]=fact[1]=1;
for(int i=2;i<N;i++)
{
fact[i]=fact[i-1]*i;
fact[i]%=mod;
}
invfact[N-1]=modinv(fact[N-1]);
for(int i=N-2;i>=0;i--)
{
invfact[i]=invfact[i+1]*(i+1);
invfact[i]%=mod;
}
}
int nCr(int x, int y)
{
if(y>x)
return 0;
int num=fact[x];
num*=invfact[y];
num%=mod;
num*=invfact[x-y];
num%=mod;
return num;
}
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b)
{
return (a.second < b.second);
}
bool ssort(const vector<int> &a, const vector<int> &b){
return a[1] > b[1];
}
string toBinary(int n)
{
string r;
while (n != 0)
{
r = (n % 2 == 0 ? "0" : "1") + r;
n /= 2;
}
return r;
}
void sieve()
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
/* int dx[] = {1,-1,0,0} , dy[] = {0,0,1,-1}; */ // 4 Direction
/* int dx[] = {1,-1,0,0,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1,1,-1}; */ // 8 Direction
/* int dx[] = {1,-1,1,-1,2,2,-2,-2} , dy[] = {2,2,-2,-2,1,-1,1,-1}; */ // Knight Direction
/* int dx[] = {2,-2,1,1,-1,-1} , dy[] = {0,0,1,-1,1,-1}; */ // Hexagonal Direction
double distance(int a, int b){
return sqrt(a*a + b*b);
}
void nextPermutation(vector<int>& arr)
{
int n = arr.size(), i, j;
// Find for the pivot element.
// A pivot is the first element from
// end of sequencewhich doesn't follow
// property of non-increasing suffix
for (i = n - 2; i >= 0; i--) {
if (arr[i] < arr[i + 1]) {
break;
}
}
// Check if pivot is not found
if (i < 0) {
reverse(arr.begin(), arr.end());
}
// if pivot is found
else {
// Find for the successor of pivot in suffix
for (j = n - 1; j > i; j--) {
if (arr[j] > arr[i]) {
break;
}
}
// Swap the pivot and successor
swap(arr[i], arr[j]);
// Minimise the suffix part
reverse(arr.begin() + i + 1, arr.end());
}
}
void solve(int in)
{
int ans = 0;
int n, e;
cin>>n>>e;
vector<vector<int>> a(n+1, vector<int> (3));
a[0][0] = 0, a[0][1] = 1e18, a[0][2] = 0;
// a[0] = {0ll,1e18,0ll};
forp(i,1,n+1){
forp(j,0,3) cin>>a[i][j];
}
sort(all(a), ssort);
deb(a);
map<int, vector<vector<int>>, greater<int>> m;
forp(i,0,n+1){
m[a[i][1]].pb({a[i][0], a[i][2], LLONG_MIN, LLONG_MIN});
}
for(auto it = m.begin(); it != m.end(); it++){
sort(all(m[it->first]));
forp(i,0,it->second.size()){
for(auto iter = m.begin(); iter != it; iter++){
forp(j,0,iter->second.size()){
if(iter->second[j][0] < it->second[i][0]){
it->second[i][2] = max({it->second[i][2], iter->second[j][2] + it->second[i][1], iter->second[j][3] + it->second[i][1] - e });
it->second[i][3] = max(it->second[i][2] - e, iter->second[j][3]);
}
else if(iter->second[j][0] > it->second[i][0]){
it->second[i][3] = max({it->second[i][3], iter->second[j][3] + it->second[i][1], iter->second[j][2] + it->second[i][1] - e });
it->second[i][2] = max(it->second[i][3] - e, iter->second[j][2]);
}
else{
it->second[i][2] = max({it->second[i][2], iter->second[j][2] + it->second[i][1], iter->second[j][3] + it->second[i][1] - e });
it->second[i][3] = max({it->second[i][3], iter->second[j][3] + it->second[i][1], iter->second[j][2] + it->second[i][1] - e });
}
}
}
ans = max({ans, it->second[i][2], it->second[i][3]});
}
it->second[0][2] = max(it->second[0][2], it->second[0][1]);
forp(i,1,it->second.size()){
it->second[i][2] = max({it->second[i][2], it->second[i-1][2] + it->second[i][1], it->second[i-1][3] + it->second[i][1] - e});
ans = max({ans, it->second[i][2], it->second[i][3]});
}
it->second.back()[3] = max(it->second.back()[3], it->second.back()[1] - e);
// deb("hi");
for(int i = it->second.size() - 2; i >= 0; i--){
it->second[i][3] = max({it->second[i][3], it->second[i+1][3] + it->second[i][1], it->second[i+1][2] + it->second[i][1] - e});
ans = max({ans, it->second[i][2], it->second[i][3]});
}
// deb(it->first);
// deb(it->second);
}
// vector<pair<int, int>> v(n+1);
// v[0] = {0,-e};
// forp(i,1,n+1){
// v[i] = {LLONG_MIN, LLONG_MIN};
// forp(j,0,i){
// if(a[j][0] < a[i][0]){
// v[i].first = max({v[i].first, v[j].first + a[i][2], v[j].second + a[i][2] - e});
// v[i].second = max(v[i].second, v[i].first - e);
// }
// else if(a[j][0] > a[i][0]){
// v[i].second = max({v[i].second, v[j].second + a[i][2], v[j].first + a[i][2] - e});
// v[i].first = max(v[i].first, v[i].second - e);
// }
// // else{
// // v[i].first = max({v[i].first, v[j].first + a[i][2], v[j].second + a[i][2] - e});
// // v[i].second = max({v[i].second, v[j].second + a[i][2], v[j].first + a[i][2] - e});
// // }
// }
// ans = max({ans, v[i].first, v[i].second});
// // deb(i, ans);jj
// }
// forp(i,1,n+1){
// // v[i] = {LLONG_MIN, LLONG_MIN};
// forp(j,0,n+1){
// if(a[j][1] > a[i][1]) break;
// if(a[j][0] < a[i][0]){
// v[i].first = max({v[i].first, v[j].first + a[i][2], v[j].second + a[i][2] - e});
// v[i].second = max(v[i].second, v[i].first - e);
// }
// else if(a[j][0] > a[i][0]){
// v[i].second = max({v[i].second, v[j].second + a[i][2], v[j].first + a[i][2] - e});
// v[i].first = max(v[i].first, v[i].second - e);
// }
// else{
// v[i].first = max({v[i].first, v[j].first + a[i][2], v[j].second + a[i][2] - e});
// v[i].second = max({v[i].second, v[j].second + a[i][2], v[j].first + a[i][2] - e});
// }
// }
// ans = max({ans, v[i].first, v[i].second});
// // deb(i, ans);
// }
// do{
// // ans++;
// int x = a[perm[0]-1][0], y = a[perm[0]-1][1], cur = a[perm[0]-1][2];
// ans = max(ans, cur);
// int dir = 1;
// forp(i,1,perm.size()){
// int cx = a[perm[i]-1][0], cy = a[perm[i]-1][1], cc = a[perm[i]-1][2];
// if(cy <= y){
// if(cx < x && dir == 1){
// cur -= e;
// dir = 0;
// }
// else if(cx > x && dir == 0){
// cur -= e;
// dir = 1;
// }
// cur += cc;
// x = cx;
// y = cy;
// }
// ans = max(ans, cur);
// }
// nextPermutation(perm);
// // deb(perm);
// } while(!is_sorted(all(perm)));
cout<<"Case #"<<in<<": "<<ans<<endl;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int t = 1;
cin >> t;
forp(i, 1, t + 1)
{
solve(i);
}
return 0;
}