#include <bits/stdc++.h>
using namespace std;
/* struct Line {
long long a, b;
Line(long long _a, long long _b) : a(_a), b(_b) {}
bool operator <(const Line& o) const { return a > o.a; }
long long eval(long long x) const { return a*x + b; }
};
// queries are decreasing so we can always query the last value
struct CHT : multiset<Line> {
bool hasNext(multiset<Line>::iterator it) { return next(it) != this->end(); }
bool hasPrev(multiset<Line>::iterator it) { return it != this->begin(); }
long double intersect(Line x, Line y) { return ((long double)(x.a-y.a)) / (y.b-x.b); }
bool bad(Line a, Line b, Line c) { return intersect(a, c) <= intersect(a, b); }
void add(Line l) {
auto it = this->lower_bound(l);
if(it != this->end() && it->a == l.a) {
if(it->b < l.b) return;
this->erase(it);
}
it = this->insert(l);
if(hasNext(it) && hasPrev(it) && bad(*prev(it), *it, *next(it)))
return (void)(this->erase(it));
while(hasNext(it) && hasNext(next(it)) && bad(*it, *next(it), *next(next(it))))
this->erase(next(it));
while(hasPrev(it) && hasPrev(prev(it)) && bad(*prev(prev(it)), *prev(it), *it))
this->erase(prev(it));
}
long long query(long long x) {
// long long ans = 0x3f3f3f3f3f3f3f3f;
// for(Line l : *this)
// ans = min(ans, l.eval(x));
// return ans;
if((this->size()) == 0) return 0x3f3f3f3f3f3f3f3f;
while(this->size() >= 2 && prev(this->end())->eval(x) >= prev( prev(this->end()) )->eval(x))
this->erase(prev(this->end()));
return prev(this->end())->eval(x);
}
} cht; */
constexpr long long inf = 0x3f3f3f3f3f3f3f3f;
struct Line
{
long long a, b, x; // vou codar com inteiros msm
Line(long long _a = 0, long long _b = 0, long long _x = inf) : a(_a), b(_b), x(_x) {}
bool operator<(const Line& o) const { return a > o. a; } // change < for >, for min queries
bool operator<(const long long& o) const { return x < o; }
};
struct LineContainer : multiset<Line, less<>>
{
bool hasNext(iterator it) { return next(it) != end(); }
bool hasPrev(iterator it) { return it != begin(); }
long double intersect(Line l1, Line l2) { return ((long double)(l1.b-l2.b)) / (l2.a-l1.a); }
bool bad(Line l1, Line l2, Line l3) { return intersect(l1, l3) <= intersect(l1, l2); }
iterator get_x(iterator it) {
if(!hasNext(it)) return it;
Line l = *it;
l.x = intersect(l, *next(it));
erase(it);
return insert(l);
}
void add(Line l) {
auto it = lower_bound(l);
long long a = l.a, b = l.b;
if(it != end() && it->a == a) {
if(it->b < b) return;
erase(it);
}
it = insert(l);
if(hasPrev(it) && hasNext(it) && bad(*prev(it), *it, *next(it)))
return (void)(erase(it));
while(hasPrev(it) && hasPrev(prev(it)) && bad(*prev(prev(it)), *prev(it), *it))
erase(prev(it));
while(hasNext(it) && hasNext(next(it)) && bad(*it, *next(it), *next(next(it))))
erase(next(it));
it = get_x(it);
if(hasPrev(it)) get_x(prev(it));
}
long long query(long long x) {
// assert(!empty());
if(empty()) return 0x3f3f3f3f3f3f3f3f;
auto it = lower_bound(x);
return it->a*x + it->b;
}
} cht;
/* using ll = long long;
struct Line {
mutable ll k, m, p;
Line(ll a, ll b, ll c = 0) : k(a), m(b), p(c) {}
bool operator<(const Line& o) const { return k > o.k; }
bool operator<(ll x) const { return p < x; }
};
struct LineContainer : multiset<Line, less<>> {
// (for doubles, use inf = 1/.0, div(a,b) = a/b)
static const ll inf = LLONG_MAX;
ll div(ll a, ll b) { // floored division
return a / b - ((a ^ b) < 0 && a % b); }
bool isect(iterator x, iterator y) {
if (y == end()) return x->p = inf, 0;
if (x->k == y->k) x->p = x->m > y->m ? inf : -inf;
else x->p = div(y->m - x->m, x->k - y->k);
return x->p >= y->p;
}
void add(Line Ln) {
ll k = Ln.k, m = Ln.m;
auto z = insert({k, m, 0}), y = z++, x = y;
while (isect(y, z)) z = erase(z);
if (x != begin() && isect(--x, y)) isect(x, y = erase(y));
while ((y = x) != begin() && (--x)->p >= y->p)
isect(x, erase(y));
}
ll query(ll x) {
assert(!empty());
auto l = *lower_bound(x);
return l.k * x + l.m;
}
} cht; */
constexpr int maxn = 2e5+10;
int nxt[maxn]; // next living guy after a water refill
long long X, N, M, W, T;
long long refill[maxn], time_person[maxn], kill_person[maxn], dp[maxn];
int main() {
scanf("%lld %lld %lld %lld %lld", &X, &N, &M, &W, &T);
for(int i = 1; i <= N; i++)
scanf("%lld", &refill[i]);
refill[++N] = X;
sort(refill+1, refill+1+N, [&](long long x1, long long x2) { return x1%T > x2%T; });
for(int i = 1; i <= M; i++)
scanf("%lld %lld", &time_person[i], &kill_person[i]);
vector<pair<long long, long long>> tempos; // o tempo T é o fim
for(int i = 1; i <= M; i++)
tempos.push_back({time_person[i], kill_person[i]});
sort(tempos.begin(), tempos.end());
for(int i = 0; i < M; i++)
time_person[i+1] = tempos[i].first, kill_person[i+1] = tempos[i].second;
time_person[M+1] = T; // setto o valor do piloto
for(int i = 1; i <= N; i++)
nxt[i] = lower_bound(time_person+1, time_person+1+M, refill[i] % T) - time_person; // ID do proximo cara vivo dps de mim
for(int i = 2; i <= M; i++)
kill_person[i] += kill_person[i-1];
auto qtd = [&](long long tempo) { return X / T + (tempo < X%T); };
dp[M+1] = W * (X/T + 1);
for(int i = M, j = 1; i >= 1; i--) {
// cht
// val[j] + pref[i-1] = {dp[nxt[j]] + pref[nxt[j]-1] + (refill[j]/T) * W * (r+1)} + l * {- (refill[j] / T) * W}
for(; j <= N && refill[j] % T >= time_person[i]; j++)
cht.add(Line(-1 * (refill[j] / T) * W, dp[nxt[j]] + kill_person[nxt[j]-1] + (refill[j]/T)*W*((nxt[j]-1)+1) ));
// opção 1: fico vivo até o final
// opção 2: uso alguem pra me matar
dp[i] = min(dp[i+1] + W * qtd(time_person[i]), cht.query(i) - kill_person[i-1]);
}
printf("%lld\n", dp[1]);
}
Compilation message
coach.cpp: In function 'int main()':
coach.cpp:143:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | scanf("%lld %lld %lld %lld %lld", &X, &N, &M, &W, &T);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
coach.cpp:145:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
145 | scanf("%lld", &refill[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
coach.cpp:151:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
151 | scanf("%lld %lld", &time_person[i], &kill_person[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
356 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
308 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
356 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
308 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
308 KB |
Output is correct |
38 |
Correct |
3 ms |
468 KB |
Output is correct |
39 |
Correct |
2 ms |
468 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
3 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
3 ms |
468 KB |
Output is correct |
48 |
Correct |
3 ms |
448 KB |
Output is correct |
49 |
Correct |
2 ms |
584 KB |
Output is correct |
50 |
Correct |
2 ms |
444 KB |
Output is correct |
51 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
356 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
308 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
308 KB |
Output is correct |
38 |
Correct |
3 ms |
468 KB |
Output is correct |
39 |
Correct |
2 ms |
468 KB |
Output is correct |
40 |
Correct |
3 ms |
468 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
3 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
2 ms |
468 KB |
Output is correct |
47 |
Correct |
3 ms |
468 KB |
Output is correct |
48 |
Correct |
3 ms |
448 KB |
Output is correct |
49 |
Correct |
2 ms |
584 KB |
Output is correct |
50 |
Correct |
2 ms |
444 KB |
Output is correct |
51 |
Correct |
2 ms |
468 KB |
Output is correct |
52 |
Correct |
206 ms |
16248 KB |
Output is correct |
53 |
Correct |
253 ms |
16432 KB |
Output is correct |
54 |
Correct |
213 ms |
15444 KB |
Output is correct |
55 |
Correct |
227 ms |
15584 KB |
Output is correct |
56 |
Correct |
222 ms |
15924 KB |
Output is correct |
57 |
Correct |
207 ms |
15720 KB |
Output is correct |
58 |
Correct |
224 ms |
16284 KB |
Output is correct |
59 |
Correct |
232 ms |
15656 KB |
Output is correct |
60 |
Correct |
232 ms |
15476 KB |
Output is correct |
61 |
Correct |
209 ms |
15656 KB |
Output is correct |
62 |
Correct |
216 ms |
15660 KB |
Output is correct |
63 |
Correct |
287 ms |
20684 KB |
Output is correct |
64 |
Correct |
149 ms |
16416 KB |
Output is correct |
65 |
Correct |
303 ms |
16392 KB |
Output is correct |
66 |
Correct |
339 ms |
16360 KB |
Output is correct |
67 |
Correct |
288 ms |
16440 KB |
Output is correct |
68 |
Correct |
301 ms |
16300 KB |
Output is correct |
69 |
Correct |
215 ms |
16176 KB |
Output is correct |
70 |
Correct |
214 ms |
16192 KB |
Output is correct |
71 |
Correct |
251 ms |
16028 KB |
Output is correct |
72 |
Correct |
208 ms |
16076 KB |
Output is correct |
73 |
Correct |
241 ms |
16100 KB |
Output is correct |
74 |
Correct |
234 ms |
16140 KB |
Output is correct |
75 |
Correct |
229 ms |
16228 KB |
Output is correct |
76 |
Correct |
255 ms |
16228 KB |
Output is correct |
77 |
Correct |
183 ms |
15928 KB |
Output is correct |
78 |
Correct |
231 ms |
16220 KB |
Output is correct |
79 |
Correct |
207 ms |
15680 KB |
Output is correct |
80 |
Correct |
209 ms |
15716 KB |
Output is correct |