Submission #478038

# Submission time Handle Problem Language Result Execution time Memory
478038 2021-10-05T08:53:50 Z pure_mem Long Distance Coach (JOI17_coach) C++14
100 / 100
350 ms 21464 KB
#include <bits/stdc++.h>

#define X first
#define Y second
#define ll long long 
#define MP make_pair

using namespace std;

const int MAXN = 2e5 + 5;
const ll INF = 1e18;

struct Line {
	ll k, b;
	mutable function<const Line* ()> getNext;
	ll get(ll x) {
		return k * x + b;
	}
	double inters(const Line& oth) const {
		return (b - oth.b) * 1.0 / (oth.k - k);
	}
	bool operator< (const Line& oth) const {
		if(oth.k != INF) return k > oth.k;
		const Line* nxt = getNext();
		if(nxt == 0) return 0;
		return b - nxt->b >= (nxt->k - k) * oth.b;
	}
};

struct CHT: public multiset<Line> {
	bool bad(iterator y) {
		auto z = next(y);
		if(y == begin()) {
			if(z == end()) return 0;
			return y->k == z->k && y->b >= z->b;
		}
		auto x = prev(y);
		if (z == end()) return y->k == x->k && y->b >= x->b;
		return 1.0 * (x->b - y->b) * (z->k - x->k) >= 1.0 * (x->b - z->b) * (y->k - x->k);
	}
	void add(ll k, ll b) {
		auto it = insert((Line){k, b});
		it->getNext = [=] { return next(it) == end() ? 0 : &*next(it); };
		if(bad(it)) return void(erase(it));
		while(next(it) != end() && bad(next(it))) erase(next(it));
		while(it != begin() && bad(prev(it))) erase(prev(it));
	}
	ll get(ll x) {
		Line me = *lower_bound((Line){INF, x});
		return me.get(x);
	}
}cht;

int n, m;
ll len, w, t, water[MAXN], dp[MAXN], pr[MAXN];
pair<ll, ll> men[MAXN];

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0), cout.tie(0);

	cin >> len >> n >> m >> w >> t;

	for(int i = 1;i <= n;i++)
		cin >> water[i];
	water[++n] = len;
	sort(water + 1, water + n + 1, [](ll a, ll b) {return a % t < b % t;});
	for(int i = 1;i <= m;i++)
		cin >> men[i].X >> men[i].Y;
	sort(men + 1, men + m + 1);
	for(int i = m;i >= 1;i--)
		pr[i] = pr[i + 1] + men[i].Y;
	for(int p1 = n, p2 = m;p2;p2--) {
		while(p1 && water[p1] % t > men[p2].X) {
			ll cost = water[p1] / t * w;
			cht.add(-cost, dp[p2 + 1] - pr[p2 + 1] + (p2 + 1) * cost);
			p1--;
		}
		dp[p2] = dp[p2 + 1] + (len - men[p2].X) / t * w + w;
		if(!cht.empty())
			dp[p2] = min(dp[p2], cht.get(p2) + pr[p2]);
		//cerr << p2 << " " << dp[p2] << "\n";
	}
	cout << dp[1] + len / t * w + w;
	return 0;
}

# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 0 ms 336 KB Output is correct
26 Correct 0 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 0 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 0 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 328 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 0 ms 336 KB Output is correct
26 Correct 0 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 0 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 0 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 328 KB Output is correct
38 Correct 2 ms 464 KB Output is correct
39 Correct 2 ms 464 KB Output is correct
40 Correct 2 ms 464 KB Output is correct
41 Correct 2 ms 464 KB Output is correct
42 Correct 2 ms 464 KB Output is correct
43 Correct 2 ms 464 KB Output is correct
44 Correct 2 ms 464 KB Output is correct
45 Correct 2 ms 460 KB Output is correct
46 Correct 2 ms 464 KB Output is correct
47 Correct 2 ms 464 KB Output is correct
48 Correct 2 ms 464 KB Output is correct
49 Correct 2 ms 464 KB Output is correct
50 Correct 2 ms 336 KB Output is correct
51 Correct 2 ms 464 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 332 KB Output is correct
2 Correct 0 ms 332 KB Output is correct
3 Correct 0 ms 332 KB Output is correct
4 Correct 1 ms 332 KB Output is correct
5 Correct 1 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 0 ms 332 KB Output is correct
8 Correct 0 ms 332 KB Output is correct
9 Correct 0 ms 332 KB Output is correct
10 Correct 0 ms 332 KB Output is correct
11 Correct 0 ms 332 KB Output is correct
12 Correct 0 ms 332 KB Output is correct
13 Correct 0 ms 332 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 0 ms 332 KB Output is correct
16 Correct 0 ms 332 KB Output is correct
17 Correct 0 ms 332 KB Output is correct
18 Correct 1 ms 332 KB Output is correct
19 Correct 0 ms 332 KB Output is correct
20 Correct 0 ms 332 KB Output is correct
21 Correct 1 ms 332 KB Output is correct
22 Correct 1 ms 336 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 0 ms 336 KB Output is correct
26 Correct 0 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 0 ms 336 KB Output is correct
29 Correct 1 ms 336 KB Output is correct
30 Correct 1 ms 336 KB Output is correct
31 Correct 0 ms 336 KB Output is correct
32 Correct 1 ms 336 KB Output is correct
33 Correct 1 ms 336 KB Output is correct
34 Correct 1 ms 336 KB Output is correct
35 Correct 1 ms 336 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 328 KB Output is correct
38 Correct 2 ms 464 KB Output is correct
39 Correct 2 ms 464 KB Output is correct
40 Correct 2 ms 464 KB Output is correct
41 Correct 2 ms 464 KB Output is correct
42 Correct 2 ms 464 KB Output is correct
43 Correct 2 ms 464 KB Output is correct
44 Correct 2 ms 464 KB Output is correct
45 Correct 2 ms 460 KB Output is correct
46 Correct 2 ms 464 KB Output is correct
47 Correct 2 ms 464 KB Output is correct
48 Correct 2 ms 464 KB Output is correct
49 Correct 2 ms 464 KB Output is correct
50 Correct 2 ms 336 KB Output is correct
51 Correct 2 ms 464 KB Output is correct
52 Correct 206 ms 13896 KB Output is correct
53 Correct 205 ms 13896 KB Output is correct
54 Correct 199 ms 12996 KB Output is correct
55 Correct 223 ms 13220 KB Output is correct
56 Correct 201 ms 13508 KB Output is correct
57 Correct 194 ms 13200 KB Output is correct
58 Correct 205 ms 13764 KB Output is correct
59 Correct 204 ms 13308 KB Output is correct
60 Correct 202 ms 13000 KB Output is correct
61 Correct 200 ms 13120 KB Output is correct
62 Correct 199 ms 13256 KB Output is correct
63 Correct 350 ms 21464 KB Output is correct
64 Correct 135 ms 13896 KB Output is correct
65 Correct 258 ms 13948 KB Output is correct
66 Correct 260 ms 13892 KB Output is correct
67 Correct 246 ms 13800 KB Output is correct
68 Correct 249 ms 13768 KB Output is correct
69 Correct 191 ms 13604 KB Output is correct
70 Correct 190 ms 13548 KB Output is correct
71 Correct 197 ms 13640 KB Output is correct
72 Correct 194 ms 13536 KB Output is correct
73 Correct 203 ms 13636 KB Output is correct
74 Correct 200 ms 13640 KB Output is correct
75 Correct 206 ms 13704 KB Output is correct
76 Correct 210 ms 13792 KB Output is correct
77 Correct 181 ms 13516 KB Output is correct
78 Correct 186 ms 13768 KB Output is correct
79 Correct 183 ms 13156 KB Output is correct
80 Correct 189 ms 13164 KB Output is correct