#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 left_min = LLONG_MAX / 3;
ll right_min = LLONG_MAX / 3;
ll l = 0;
ll r = a.size() - 1;
while (l <= r) {
if (a[l].value > a[r].value) {
if (a[l].value < left_min) {
ret.push_back(a[l]);
left_min = a[l].value;
}
l++;
}
else {
if (a[r].value < right_min || r == 0) {
ret.push_back(a[r]);
right_min = a[r].value;
}
r--;
}
}
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)
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
6 ms |
8284 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
8284 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 |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Incorrect |
2 ms |
2908 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
6 ms |
8284 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
5 ms |
8284 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 |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
29 |
Halted |
0 ms |
0 KB |
- |