//https://oj.uz/problem/view/JOI17_coach
#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
typedef pair<ll, ll> pi;
const ll inf = 2e18;
struct line{
ll m, b;
ll eval(ll x){
return m*x+b;
}
bool cover(line a, ll l, ll r){
return eval(l) <= a.eval(l) && eval(r) <= a.eval(r);
}
line(){
m = 0; b = inf;
}
};
const int mx = 2e5+5;
struct lc{
lc *c[2]; line S;
lc(){
c[0] = c[1] = NULL; S = line();
}
void ex(int i){
if(!c[i]) c[i] = new lc();
}
void add(line X, int l = 0, int r = mx){
if(X.cover(S,l,r)){
swap(X,S);
}
if(S.cover(X,l,r)) return;
if(l == r) return;
int mid = (l+r)>>1;
if(X.eval(mid) <= S.eval(mid)) swap(X,S);
if(X.eval(l) <= S.eval(l)) ex(0), c[0]->add(X,l,mid);
else ex(1), c[1]->add(X,mid+1,r);
}
ll query(int pos, int l = 0, int r = mx){
ll ans = S.eval(pos);
int mid = (l+r)>>1;
if(pos <= mid){
if(c[0]) ans = min(ans,c[0]->query(pos, l, mid));
}else{
if(c[1]) ans = min(ans,c[1]->query(pos, mid+1, r));
}
return ans;
}
}chull;
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
//remember to include destination in dp
//remember to add driver cost
ll x, w, t; int n,m; cin >> x >> n >> m >> w >> t; //Total time, cities, people, water cost, time interval
vector<ll> stops(n+1); //where the coach stops
for(int i = 0; i < n; i++) cin >> stops[i];
stops[n] = x;
sort(stops.begin(), stops.end());
vector<pi> passengers(m+1); // water time, refund cost
for(int i = 1; i <= m; i++) cin >> passengers[i].f >> passengers[i].s;
sort(passengers.begin(), passengers.end());
//find from where you can go back on a passenger
vector<int> goback(m+1,-1);
for(int i = n; i >= 0; i--){
ll pos = stops[i]%t;
pi fnd = {pos,0};
int cur = lower_bound(passengers.begin(), passengers.end(), fnd) - passengers.begin()-1; //passenger you can go back from
goback[cur] = i;
}
//find coefficient for each stop
vector<ll> coe(n+1);
for(int i = 0; i <= n; i++) coe[i] = w*(stops[i]/t);
//find full cost for each passenger
vector<ll> cost(m+1);
for(int i = 0; i <= m; i++){
cost[i] = w*((x-passengers[i].f)/t+1LL)-passengers[i].s;
}
ll ans = 0;
//assume you refund everyone
for(int i = 0; i <= m; i++) ans += passengers[i].s;
//add driver cost
ans += cost[0];
vector<ll> dp(m+2);
//do dp
for(int i = m; i > 0; i--){
dp[i] = cost[i]+dp[i+1];
if(goback[i] != -1){
ll m1 = coe[goback[i]];
ll b1 = dp[i+1] - m1*(ll)(m-i-1);
line cur; cur.m = m1, cur.b = b1;
chull.add(cur,0,m);
}
dp[i] = min(dp[i], chull.query(m-i,0,m));
}
//give ans
cout << ans+dp[1] << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
312 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
312 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
2 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
460 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
0 ms |
204 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
0 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
1 ms |
204 KB |
Output is correct |
26 |
Correct |
1 ms |
204 KB |
Output is correct |
27 |
Correct |
0 ms |
204 KB |
Output is correct |
28 |
Correct |
0 ms |
204 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
0 ms |
312 KB |
Output is correct |
31 |
Correct |
1 ms |
204 KB |
Output is correct |
32 |
Correct |
0 ms |
204 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
204 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
2 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
460 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
1 ms |
332 KB |
Output is correct |
52 |
Correct |
143 ms |
11852 KB |
Output is correct |
53 |
Correct |
152 ms |
18624 KB |
Output is correct |
54 |
Correct |
106 ms |
15408 KB |
Output is correct |
55 |
Correct |
107 ms |
15552 KB |
Output is correct |
56 |
Correct |
106 ms |
15880 KB |
Output is correct |
57 |
Correct |
109 ms |
15672 KB |
Output is correct |
58 |
Correct |
105 ms |
16200 KB |
Output is correct |
59 |
Correct |
106 ms |
15568 KB |
Output is correct |
60 |
Correct |
108 ms |
15364 KB |
Output is correct |
61 |
Correct |
108 ms |
15548 KB |
Output is correct |
62 |
Correct |
112 ms |
15556 KB |
Output is correct |
63 |
Correct |
106 ms |
23896 KB |
Output is correct |
64 |
Correct |
92 ms |
16320 KB |
Output is correct |
65 |
Correct |
141 ms |
18456 KB |
Output is correct |
66 |
Correct |
143 ms |
17960 KB |
Output is correct |
67 |
Correct |
144 ms |
18504 KB |
Output is correct |
68 |
Correct |
141 ms |
18568 KB |
Output is correct |
69 |
Correct |
160 ms |
17420 KB |
Output is correct |
70 |
Correct |
156 ms |
17480 KB |
Output is correct |
71 |
Correct |
149 ms |
17428 KB |
Output is correct |
72 |
Correct |
144 ms |
17344 KB |
Output is correct |
73 |
Correct |
142 ms |
17284 KB |
Output is correct |
74 |
Correct |
166 ms |
16964 KB |
Output is correct |
75 |
Correct |
154 ms |
16568 KB |
Output is correct |
76 |
Correct |
137 ms |
16460 KB |
Output is correct |
77 |
Correct |
114 ms |
15940 KB |
Output is correct |
78 |
Correct |
111 ms |
16144 KB |
Output is correct |
79 |
Correct |
147 ms |
16968 KB |
Output is correct |
80 |
Correct |
142 ms |
16964 KB |
Output is correct |