#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
const ll MAXN = 2e5+1;
ll n, m, t, wint, wcost;
ll dp[MAXN];
pii ppl[MAXN]; // time, drop_cost
ll refill[MAXN];
ll stat_with[MAXN];
ll pre[MAXN];
bool skip[MAXN];
const ll inf = 4e18;
class Frac {
public:
ll num, denom;
Frac() {
num = inf;
denom = 1;
}
Frac(ll x, ll y) {
num = x;
denom = y;
reduce();
}
void reduce() {
if (denom < 0) {
denom *= -1;
num *= -1;
}
ll g = gcd(abs(num), denom);
num /= g;
denom /= g;
}
ll gcd(ll a, ll b) {
if (a > b) return gcd(b, a);
if (a == 0) return b;
return gcd(b%a, a);
}
};
bool operator<(Frac a, Frac b) {
return ((__int128)a.num*b.denom) < ((__int128)b.num*a.denom);
}
class Line {
public:
ll inter;
ll slope;
Frac rbound;
Line(ll in, ll s) {
inter = in;
slope = s;
}
};
Frac intersect(Line a, Line b) {
return Frac(a.inter-b.inter, b.slope-a.slope);
}
ll sim(vector<int> &amounts) {
fill(skip, skip+n, 0);
int apos = 1;
int iter = 0;
int cur = 0;
int w = amounts[0];
ll cost = 0;
while (iter*wint+ppl[cur].first < t) {
while (apos < m && refill[apos-1] < iter*wint+ppl[cur].first) {
w += amounts[apos];
apos++;
}
if (!skip[cur]) {
if (w == 0) {
if (cur == 0) return -1;
skip[cur] = 1;
cost += ppl[cur].second;
}
else {
w--;
cost += wcost;
}
}
cur++;
if (cur == n) {
iter++;
cur = 0;
}
}
return cost;
}
ll opt(int pos, ll w_left, vector<int> &amounts) {
if (pos == m) return sim(amounts);
ll best = -1;
for (int a = 0; a <= w_left; a++) {
amounts.push_back(a);
ll cur = opt(pos+1, w_left-a, amounts);
amounts.pop_back();
if (cur != -1 && (best == -1 || cur < best)) best = cur;
}
return best;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(NULL);
cin >> t >> m >> n >> wcost >> wint;
m++;
for (ll i = 0; i < m-1; i++) {
long long r; cin >> r;
refill[i] = r;
}
refill[m-1] = t;
sort(refill, refill+m);
n++;
ppl[0] = pii(0, 0);
for (ll i = 1; i < n; i++) {
ll x, y; cin >> x >> y;
ppl[i] = pii(x, y);
}
sort(ppl, ppl+n);
fill(stat_with, stat_with+n, -1);
ll cur = 0;
ll mxw = 0;
for (ll i = 0; i < n; i++) {
cur += ppl[i].second;
pre[i] = cur;
mxw += (t-ppl[i].first)/wint + 1;
}
for (ll i = 0; i < m; i++) {
ll comp = refill[i]/wint+1;
ll mn = 0;
ll mx = n;
while (mx > mn+1) {
ll cur = (mn+mx)/2;
if ((refill[i]-ppl[cur].first+wint)/wint == comp) mn = cur;
else mx = cur;
}
if (stat_with[mn] == -1) stat_with[mn] = i;
}
dp[0] = wcost*(t/wint+1);
vector<Line> hull;
hull.push_back(Line(dp[0]-pre[0], 0));
for (ll i = 1; i < n; i++) {
dp[i] = wcost*((t-ppl[i].first)/wint+1)+dp[i-1];
if (stat_with[i] >= 0) {
ll stat = stat_with[i];
ll sl = wcost*((refill[stat]-ppl[i].first)/wint);
ll mn = -1;
ll mx = hull.size()-1;
Frac f(sl, 1);
while (mx > mn+1) {
ll cur = (mn+mx)/2;
if (hull[cur].rbound < f) mn = cur;
else mx = cur;
}
ll cval = pre[i]+hull[mx].inter+hull[mx].slope*sl+i*sl;
dp[i] = min(dp[i], cval);
}
Line cur_l(dp[i]-pre[i], -i);
Frac inter = intersect(cur_l, hull.back());
while (hull.size() > 1 && inter < hull[hull.size()-2].rbound) {
hull.pop_back();
inter = intersect(cur_l, hull.back());
}
hull.back().rbound = inter;
hull.push_back(cur_l);
}
//vector<int> v;
//assert(dp[n-1] == opt(0, mxw, v));
cout << dp[n-1] << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
328 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
328 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
2 ms |
468 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
476 KB |
Output is correct |
44 |
Correct |
2 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
2 ms |
468 KB |
Output is correct |
48 |
Correct |
2 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
476 KB |
Output is correct |
50 |
Correct |
2 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
336 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
340 KB |
Output is correct |
21 |
Correct |
0 ms |
340 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
0 ms |
328 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
0 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
340 KB |
Output is correct |
29 |
Correct |
0 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
0 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
2 ms |
468 KB |
Output is correct |
39 |
Correct |
2 ms |
468 KB |
Output is correct |
40 |
Correct |
2 ms |
596 KB |
Output is correct |
41 |
Correct |
2 ms |
468 KB |
Output is correct |
42 |
Correct |
2 ms |
468 KB |
Output is correct |
43 |
Correct |
1 ms |
476 KB |
Output is correct |
44 |
Correct |
2 ms |
468 KB |
Output is correct |
45 |
Correct |
2 ms |
468 KB |
Output is correct |
46 |
Correct |
1 ms |
596 KB |
Output is correct |
47 |
Correct |
2 ms |
468 KB |
Output is correct |
48 |
Correct |
2 ms |
468 KB |
Output is correct |
49 |
Correct |
1 ms |
476 KB |
Output is correct |
50 |
Correct |
2 ms |
468 KB |
Output is correct |
51 |
Correct |
1 ms |
468 KB |
Output is correct |
52 |
Correct |
162 ms |
15356 KB |
Output is correct |
53 |
Correct |
160 ms |
15464 KB |
Output is correct |
54 |
Correct |
138 ms |
16372 KB |
Output is correct |
55 |
Correct |
143 ms |
15688 KB |
Output is correct |
56 |
Correct |
146 ms |
15396 KB |
Output is correct |
57 |
Correct |
138 ms |
15008 KB |
Output is correct |
58 |
Correct |
147 ms |
15360 KB |
Output is correct |
59 |
Correct |
143 ms |
15560 KB |
Output is correct |
60 |
Correct |
148 ms |
15096 KB |
Output is correct |
61 |
Correct |
144 ms |
15668 KB |
Output is correct |
62 |
Correct |
140 ms |
15180 KB |
Output is correct |
63 |
Correct |
112 ms |
21444 KB |
Output is correct |
64 |
Correct |
126 ms |
15448 KB |
Output is correct |
65 |
Correct |
159 ms |
15524 KB |
Output is correct |
66 |
Correct |
150 ms |
15428 KB |
Output is correct |
67 |
Correct |
153 ms |
15448 KB |
Output is correct |
68 |
Correct |
154 ms |
15384 KB |
Output is correct |
69 |
Correct |
164 ms |
15204 KB |
Output is correct |
70 |
Correct |
172 ms |
15084 KB |
Output is correct |
71 |
Correct |
160 ms |
15108 KB |
Output is correct |
72 |
Correct |
159 ms |
15256 KB |
Output is correct |
73 |
Correct |
159 ms |
15204 KB |
Output is correct |
74 |
Correct |
163 ms |
15152 KB |
Output is correct |
75 |
Correct |
159 ms |
15316 KB |
Output is correct |
76 |
Correct |
164 ms |
15308 KB |
Output is correct |
77 |
Correct |
138 ms |
18784 KB |
Output is correct |
78 |
Correct |
147 ms |
15324 KB |
Output is correct |
79 |
Correct |
154 ms |
14700 KB |
Output is correct |
80 |
Correct |
158 ms |
14792 KB |
Output is correct |