#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<ll,ll,ll> tpl;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 1e5 + 5;
ll a[mn], b[mn], ans[mn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= m; i++) cin >> b[i];
vector<tpl> checkpoints;
checkpoints.emplace_back(0, 1, 1);
fill(ans + 1, ans + 1 + n, LLONG_MAX);
ll preOpt = 0;
for (int i = 2; i <= n; i++) {
ll optCol = 0, optVal = LLONG_MAX;
for (int j = 0; j < checkpoints.size(); j++) {
ll val, row, curCol; tie(val, row, curCol) = checkpoints[j];
ll nxtCol = (j + 1 < checkpoints.size() ? get<2>(checkpoints[j + 1]) : m + 1);
for (int col = max(preOpt, curCol); col < nxtCol; col++) {
ll curVal = val + a[row] * (col - curCol) + b[col] * (i - row);
if (curVal + a[i] * (m - col) < ans[i])
ans[i] = curVal + a[i] * (m - col), optCol = col, optVal = curVal;
// cout << curVal + a[i] * (m - col) << " ";
}
}
preOpt = optCol;
while (checkpoints.size() && get<2>(checkpoints.back()) >= optCol) checkpoints.pop_back();
checkpoints.emplace_back(optVal, i, optCol);
// cout << "row " << i << ", check-points: ";
// for (auto [a, b, c] : checkpoints) cout << a << " ";
// cout << "\n";
}
cout << ans[n];
return 0;
}
Compilation message
kyoto.cpp: In function 'int main()':
kyoto.cpp:32:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<long long int, long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
32 | for (int j = 0; j < checkpoints.size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~~
kyoto.cpp:34:32: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::tuple<long long int, long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
34 | ll nxtCol = (j + 1 < checkpoints.size() ? get<2>(checkpoints[j + 1]) : m + 1);
| ~~~~~~^~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |