Submission #283998

#TimeUsernameProblemLanguageResultExecution timeMemory
283998BertedLong Distance Coach (JOI17_coach)C++14
100 / 100
171 ms18648 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#define ll long long
#define pii pair<ll, ll>
#define fst first
#define snd second
#define ppi pair<ll, pii>
using namespace std;

/*
	Solution :
	Due to the existence of the driver, therefore we cannot remove cyclically
	i.e. 6, 7, 0, 1, 2
	Therefore, we can only remove "segments" at a time.

	Obs. 2:
	In each removal, it will be optimal to not "jump through" segments
	i.e. removal 1 removes 2, removal 2 removes 1,3 (this is not optimal)
	As if removal 1 is better than removal 2, it is optimal to place 1 in removal 1
	on the other hand, therefore it is optimal to place 2 in removal 2.	

	Therefore, we can solve it with a single state DP with O(N) transition (71 points)
	To get AC, optimize DP with CHT
*/

const ll INF = 5e18;

ll x, w, t, s[200051], pref[200051], v[200051], dp[200051], res;
int n, m;
pii d[200051];

vector<pii> lines;
vector<ll> val;

inline bool check(pii l1, pii l2, pii l3)
{
	return (long double)(l2.snd - l1.snd) / (l1.fst - l2.fst) >= (long double)(l3.snd - l2.snd) / (l2.fst - l3.fst);
}

inline ll getX(pii l1, pii l2)
{
	return ceill((long double)(l2.snd - l1.snd) / (l1.fst - l2.fst));
}

int main()
{
	ios :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> x >> n >> m >> w >> t;
	s[0] = 0;
	for (int i = 1; i <= n; i++) {cin >> s[i];}
	s[n + 1] = x;
	for (int i = 1; i <= m; i++) {cin >> d[i].fst >> d[i].snd; v[i] = INF;}
	sort(s, s + n + 2); sort(d + 1, d + m + 1);
	res += w * (s[n + 1] / t + 1);
	for (int i = 1; i <= m; i++)
	{
		pref[i] = d[i].snd;
		if (s[n + 1] >= d[i].fst) 
		{
			pref[i] -= w * ((s[n + 1] - d[i].fst) / t + 1);
			res += w * ((s[n + 1] - d[i].fst) / t + 1);
		}
		pref[i] += pref[i - 1];
	}
	for (int i = 1; i <= n + 1; i++)
	{
		if (s[i] % t > d[1].fst)
		{
			int idx = upper_bound(d + 1, d + m + 1, make_pair(s[i] % t, -1LL)) - d - 1;
			if (v[idx] == INF && (s[i] - s[i - 1] >= t || s[i - 1] % t > s[i] % t || (s[i - 1] % t < d[idx].fst && d[idx].fst < s[i] % t))) 
			{
				v[idx] = w * (s[i] / t);
			}
		}
	}
	dp[0] = 0; lines.push_back({0, 0}); val.push_back(-INF);
	for (int i = 1; i <= m; i++)
	{
		int idx = prev(upper_bound(val.begin(), val.end(), v[i])) - val.begin();
		if (v[i] < INF)
		{
			dp[i] = min(dp[i - 1], pref[i] + i * v[i] + lines[idx].fst * v[i] + lines[idx].snd);
		}
		else {dp[i] = dp[i - 1];}
		
		pii nl = {-i, dp[i] - pref[i]};
		while (lines.size() > 1 && check(lines[lines.size() - 2], lines.back(), nl)) {lines.pop_back(); val.pop_back();}
		val.push_back(getX(lines.back(), nl));
		lines.push_back(nl);
	}
	cout << res + dp[m] << "\n";
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...