Submission #113121

# Submission time Handle Problem Language Result Execution time Memory
113121 2019-05-23T20:03:19 Z Bruteforceman Long Distance Coach (JOI17_coach) C++11
100 / 100
214 ms 28084 KB
#include "bits/stdc++.h"
using namespace std;
long long s[200010];
long long d[200010];
long long c[200010];
int N, M;
long long X, W, T;
typedef pair <long long, int> pii;
vector <pii> v;
long long dp[400010];
long long p[400010];
int cnt[400010];


struct CHT {
	vector <long long> M, C;
	bool bad(int i, int j, int k) {
		return (C[k] - C[j]) * (M[i] - M[j]) <= (C[j] - C[i]) * (M[j] - M[k]);
	}
	void add_line(long long m, long long c) {
		if(!M.empty() && M.back() == m) {
			if(C.back() < c) return ;
			else {
				M.pop_back();
				C.pop_back();
			}
		}
		M.push_back(m);
		C.push_back(c);
		while(M.size() >= 3 && bad(M.size() - 3, M.size() - 2, M.size() - 1)) {
			M.erase(M.end() - 2);
			C.erase(C.end() - 2);
		}
	}
	long long search(int b, int e, long long val) {
		if(b == e) {
			return M[b] * val + C[b];
		}
		int mid = (b + e) >> 1;
		if((M[mid] * val + C[mid]) >= (M[mid + 1] * val + C[mid + 1])) {
			return search(mid + 1, e, val);
		} else {
			return search(b, mid, val);
		}
	}
	long long eval(long long x) {
		return search(0, M.size() - 1, x);
	}
} cht;

int main(int argc, char const *argv[])
{
	scanf("%lld %d %d %lld %lld", &X, &N, &M, &W, &T);
	for(int i = 1; i <= N; i++) {
		scanf("%lld", &s[i]);
	}
	s[++N] = X;
	for(int i = 1; i <= N; i++) {
		v.push_back(pii(s[i] % T, i));
	}  
	for(int i = 1; i <= M; i++) {
		scanf("%lld %lld", &d[i], &c[i]);
		v.push_back(pii(d[i], -i));
	}
	sort(v.begin(), v.end());

	long long ans = (X / T) + 1;
	for(int i = 1; i <= M; i++) {
		ans += 1 + ((X - d[i]) / T);
	}
	ans *= W;
	int sz = N + M;
	v.insert(v.begin(), pii(0, 0));


	for(int i = 1; i <= sz; i++) {
		if(v[i].second < 0) {
			int id = -v[i].second;
			p[i] = c[id] - W * (1 + ((X - d[id]) / T));
			cnt[i] = 1;
		}
		p[i] += p[i - 1];
		cnt[i] += cnt[i - 1];
	}

	cht.add_line(0, 0);

	dp[0] = 0;
	for(int i = 1; i <= sz; i++) {
		if(v[i].second < 0) {
			dp[i] = dp[i - 1];
		} else {
			long long var = W * (s[v[i].second] / T);
			long long cost = 0;
			long long add = p[i] + var * cnt[i];
			dp[i] = add + cht.eval(var);
		}
		long long m = -cnt[i];
		long long c = dp[i] - p[i];
		cht.add_line(m, c);
	}
	ans += dp[sz];
	printf("%lld\n", ans);
	return 0;
}

Compilation message

coach.cpp: In function 'int main(int, const char**)':
coach.cpp:94:14: warning: unused variable 'cost' [-Wunused-variable]
    long long cost = 0;
              ^~~~
coach.cpp:53:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%lld %d %d %lld %lld", &X, &N, &M, &W, &T);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
coach.cpp:55:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld", &s[i]);
   ~~~~~^~~~~~~~~~~~~~~
coach.cpp:62:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%lld %lld", &d[i], &c[i]);
   ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 356 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 356 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 356 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 356 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 356 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 2 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 2 ms 384 KB Output is correct
35 Correct 2 ms 384 KB Output is correct
36 Correct 2 ms 384 KB Output is correct
37 Correct 2 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 356 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 356 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 356 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 2 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 2 ms 384 KB Output is correct
35 Correct 2 ms 384 KB Output is correct
36 Correct 2 ms 384 KB Output is correct
37 Correct 2 ms 384 KB Output is correct
38 Correct 3 ms 640 KB Output is correct
39 Correct 3 ms 640 KB Output is correct
40 Correct 4 ms 640 KB Output is correct
41 Correct 3 ms 640 KB Output is correct
42 Correct 3 ms 640 KB Output is correct
43 Correct 3 ms 640 KB Output is correct
44 Correct 3 ms 640 KB Output is correct
45 Correct 3 ms 640 KB Output is correct
46 Correct 3 ms 640 KB Output is correct
47 Correct 3 ms 640 KB Output is correct
48 Correct 3 ms 640 KB Output is correct
49 Correct 3 ms 640 KB Output is correct
50 Correct 3 ms 640 KB Output is correct
51 Correct 3 ms 640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 384 KB Output is correct
2 Correct 2 ms 384 KB Output is correct
3 Correct 2 ms 384 KB Output is correct
4 Correct 2 ms 384 KB Output is correct
5 Correct 2 ms 384 KB Output is correct
6 Correct 2 ms 384 KB Output is correct
7 Correct 2 ms 356 KB Output is correct
8 Correct 2 ms 384 KB Output is correct
9 Correct 2 ms 384 KB Output is correct
10 Correct 2 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 2 ms 384 KB Output is correct
13 Correct 2 ms 356 KB Output is correct
14 Correct 2 ms 384 KB Output is correct
15 Correct 2 ms 384 KB Output is correct
16 Correct 2 ms 384 KB Output is correct
17 Correct 2 ms 384 KB Output is correct
18 Correct 2 ms 384 KB Output is correct
19 Correct 2 ms 384 KB Output is correct
20 Correct 3 ms 384 KB Output is correct
21 Correct 2 ms 384 KB Output is correct
22 Correct 2 ms 384 KB Output is correct
23 Correct 2 ms 384 KB Output is correct
24 Correct 2 ms 356 KB Output is correct
25 Correct 2 ms 384 KB Output is correct
26 Correct 2 ms 384 KB Output is correct
27 Correct 2 ms 384 KB Output is correct
28 Correct 2 ms 384 KB Output is correct
29 Correct 2 ms 384 KB Output is correct
30 Correct 2 ms 384 KB Output is correct
31 Correct 2 ms 384 KB Output is correct
32 Correct 2 ms 384 KB Output is correct
33 Correct 3 ms 384 KB Output is correct
34 Correct 2 ms 384 KB Output is correct
35 Correct 2 ms 384 KB Output is correct
36 Correct 2 ms 384 KB Output is correct
37 Correct 2 ms 384 KB Output is correct
38 Correct 3 ms 640 KB Output is correct
39 Correct 3 ms 640 KB Output is correct
40 Correct 4 ms 640 KB Output is correct
41 Correct 3 ms 640 KB Output is correct
42 Correct 3 ms 640 KB Output is correct
43 Correct 3 ms 640 KB Output is correct
44 Correct 3 ms 640 KB Output is correct
45 Correct 3 ms 640 KB Output is correct
46 Correct 3 ms 640 KB Output is correct
47 Correct 3 ms 640 KB Output is correct
48 Correct 3 ms 640 KB Output is correct
49 Correct 3 ms 640 KB Output is correct
50 Correct 3 ms 640 KB Output is correct
51 Correct 3 ms 640 KB Output is correct
52 Correct 181 ms 19356 KB Output is correct
53 Correct 168 ms 25144 KB Output is correct
54 Correct 162 ms 24068 KB Output is correct
55 Correct 169 ms 24372 KB Output is correct
56 Correct 168 ms 24656 KB Output is correct
57 Correct 214 ms 24580 KB Output is correct
58 Correct 173 ms 24968 KB Output is correct
59 Correct 165 ms 24400 KB Output is correct
60 Correct 164 ms 24160 KB Output is correct
61 Correct 175 ms 24528 KB Output is correct
62 Correct 198 ms 24372 KB Output is correct
63 Correct 145 ms 28084 KB Output is correct
64 Correct 129 ms 25172 KB Output is correct
65 Correct 166 ms 25096 KB Output is correct
66 Correct 164 ms 25196 KB Output is correct
67 Correct 167 ms 25040 KB Output is correct
68 Correct 168 ms 24956 KB Output is correct
69 Correct 183 ms 24756 KB Output is correct
70 Correct 182 ms 24972 KB Output is correct
71 Correct 194 ms 24792 KB Output is correct
72 Correct 178 ms 24784 KB Output is correct
73 Correct 202 ms 24784 KB Output is correct
74 Correct 182 ms 24916 KB Output is correct
75 Correct 196 ms 24948 KB Output is correct
76 Correct 180 ms 25012 KB Output is correct
77 Correct 172 ms 24660 KB Output is correct
78 Correct 176 ms 24972 KB Output is correct
79 Correct 180 ms 24396 KB Output is correct
80 Correct 175 ms 24396 KB Output is correct