#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
struct s {
ll pos, value;
bool operator <(s const& rhs) const {
return pos < rhs.pos;
}
};
ll solve(vector<s> const& a, vector<s> const& b) {
vector dp(a.size(), vector<ll>(b.size(), LLONG_MAX / 3));
dp[0][0] = 0;
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b.size(); j++) {
if (i < a.size() - 1)
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + b[j].value * (a[i + 1].pos - a[i].pos));
if (j < b.size() - 1)
dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + a[i].value * (b[j + 1].pos - b[j].pos));
}
}
return dp.back().back();
}
vector<s> compress(vector<s> const& a) {
vector<s> ret;
ll first_smallest = 0;
ll last_smallest = 0;
for (int i = 0; i < a.size(); i++) {
if (a[i].value < a[first_smallest].value)
first_smallest = i;
if (a[i].value <= a[last_smallest].value)
last_smallest = i;
}
ret.push_back(a[0]);
for (int i = 1; i < first_smallest; i++)
if (a[i].value < ret.back().value)
ret.push_back(a[i]);
ret.push_back(a[first_smallest]);
ret.push_back(a.back());
for (int i = a.size() - 1; i > last_smallest; i--)
if (a[i].value < ret.back().value)
ret.push_back(a[i]);
ret.push_back(a[last_smallest]);
sort(ret.begin(), ret.end());
return ret;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
ll h, w;
cin >> h >> w;
vector<s> a(h);
for (int i = 0; i < h; i++) {
a[i].pos = i;
cin >> a[i].value;
}
vector<s> b(w);
for (int i = 0; i < w; i++) {
b[i].pos = i;
cin >> b[i].value;
}
a = compress(a);
b = compress(b);
cout << solve(a, b) << "\n";
}
Compilation message
kyoto.cpp: In function 'll solve(const std::vector<s>&, const std::vector<s>&)':
kyoto.cpp:22:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<s>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
22 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
kyoto.cpp:23:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<s>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int j = 0; j < b.size(); j++) {
| ~~^~~~~~~~~~
kyoto.cpp:24:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<s>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | if (i < a.size() - 1)
| ~~^~~~~~~~~~~~~~
kyoto.cpp:27:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<s>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
27 | if (j < b.size() - 1)
| ~~^~~~~~~~~~~~~~
kyoto.cpp: In function 'std::vector<s> compress(const std::vector<s>&)':
kyoto.cpp:40:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<s>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int i = 0; i < a.size(); i++) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
7 ms |
8284 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
8284 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
424 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
2908 KB |
Output is correct |
4 |
Correct |
4 ms |
1628 KB |
Output is correct |
5 |
Correct |
5 ms |
2232 KB |
Output is correct |
6 |
Correct |
2 ms |
860 KB |
Output is correct |
7 |
Correct |
9 ms |
3568 KB |
Output is correct |
8 |
Correct |
9 ms |
3420 KB |
Output is correct |
9 |
Correct |
10 ms |
3420 KB |
Output is correct |
10 |
Correct |
10 ms |
3568 KB |
Output is correct |
11 |
Correct |
32 ms |
31772 KB |
Output is correct |
12 |
Correct |
11 ms |
3604 KB |
Output is correct |
13 |
Correct |
14 ms |
3672 KB |
Output is correct |
14 |
Correct |
33 ms |
31248 KB |
Output is correct |
15 |
Correct |
11 ms |
3420 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
424 KB |
Output is correct |
26 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
7 ms |
8284 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
344 KB |
Output is correct |
16 |
Correct |
6 ms |
8284 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
424 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
0 ms |
344 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
2908 KB |
Output is correct |
32 |
Correct |
4 ms |
1628 KB |
Output is correct |
33 |
Correct |
5 ms |
2232 KB |
Output is correct |
34 |
Correct |
2 ms |
860 KB |
Output is correct |
35 |
Correct |
9 ms |
3568 KB |
Output is correct |
36 |
Correct |
9 ms |
3420 KB |
Output is correct |
37 |
Correct |
10 ms |
3420 KB |
Output is correct |
38 |
Correct |
10 ms |
3568 KB |
Output is correct |
39 |
Correct |
32 ms |
31772 KB |
Output is correct |
40 |
Correct |
11 ms |
3604 KB |
Output is correct |
41 |
Correct |
14 ms |
3672 KB |
Output is correct |
42 |
Correct |
33 ms |
31248 KB |
Output is correct |
43 |
Correct |
11 ms |
3420 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
0 ms |
348 KB |
Output is correct |
50 |
Correct |
0 ms |
348 KB |
Output is correct |
51 |
Correct |
0 ms |
348 KB |
Output is correct |
52 |
Correct |
0 ms |
348 KB |
Output is correct |
53 |
Correct |
0 ms |
424 KB |
Output is correct |
54 |
Correct |
0 ms |
344 KB |
Output is correct |
55 |
Correct |
6 ms |
1880 KB |
Output is correct |
56 |
Correct |
0 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
2 ms |
864 KB |
Output is correct |
59 |
Correct |
14 ms |
3420 KB |
Output is correct |
60 |
Correct |
15 ms |
3424 KB |
Output is correct |
61 |
Correct |
15 ms |
3424 KB |
Output is correct |
62 |
Correct |
15 ms |
3576 KB |
Output is correct |
63 |
Runtime error |
428 ms |
1048576 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |