This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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);
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 = 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;
}
}
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 << " " << b << " " << c << ") ";
// cout << "\n";
}
cout << ans[n];
return 0;
}
Compilation message (stderr)
kyoto.cpp: In function 'int main()':
kyoto.cpp:31: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]
31 | for (int j = 0; j < checkpoints.size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~~~
kyoto.cpp:33: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]
33 | ll nxtCol = (j + 1 < checkpoints.size() ? get<2>(checkpoints[j + 1]) : m + 1);
| ~~~~~~^~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |