#include <bits/stdc++.h>
#define Y8o "SightseeinginKyoto"
#define maxn (int) 1e5 + 5
#define ll long long
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define all(x) x.begin(), x.end()
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define fi(i, a, b) for(int i = a; i <= b; i ++)
#define fid(i, a, b) for(int i = a; i >= b; i --)
//#define f first
//#define s second
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
if(fopen(Y8o".inp", "r"))
{
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
}
void ctime() {
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
int m, n;
int a[maxn], b[maxn];
namespace sub1 {
ll dp[1005][1005];
void solve() {
memset(dp, 0x3f, sizeof dp);
dp[1][1] = 0;
fi(i, 1, m) {
fi(j, 1, n) {
if(i == 1 && i == j) continue;
dp[i][j] = min( dp[i][j - 1] + a[i], dp[i - 1][j] + b[j] );
}
}
cout << dp[m][n];
}
}
namespace sub2 {
bool csub() {
int mx = 0;
fi(i, 1, m) mx = max(mx, a[i]);
fi(i, 1, n) mx = max(mx, b[i]);
return mx <= 1000;
}
int r, c;
int row[maxn], col[maxn];
int L[maxn], R[maxn];
ll dp[2005][2005];
void solve() {
memset(L, 0x3f, sizeof L);
memset(R, 0x3f, sizeof R);
fid(i, m, 1) R[i] = min(R[i + 1], a[i]);
fi(i, 1, m){
L[i] = min(L[i - 1], a[i]);
if(L[i - 1] <= a[i] && a[i] >= R[i + 1]) continue;
row[++r] = i;
}
memset(L, 0x3f, sizeof L);
memset(R, 0x3f, sizeof R);
fid(i, n, 1) R[i] = min(R[i + 1], b[i]);
fi(i, 1, n) {
L[i] = min(L[i - 1], b[i]);
if(L[i - 1] <= b[i] && b[i] >= R[i + 1]) continue;
col[++c] = i;
}
memset(dp, 0x3f, sizeof dp);
dp[1][1] = 0;
fi(i, 1, r) {
fi(j, 1, c) {
if(i == 1 && j == i) continue;
dp[i][j] = min(dp[i - 1][j] + 1ll * (row[i] - row[i - 1]) * b[col[j]],
dp[i][j - 1] + 1ll * (col[j] - col[j - 1]) * a[row[i]]);
}
}
cout << dp[r][c];
}
}
void solve() {
cin >> m >> n;
fi(i, 1, m) cin >> a[i];
fi(i, 1, n) cin >> b[i];
// if(max(m, n) <= 1000) return sub1 :: solve(), void();
if(sub2 :: csub()) return sub2 :: solve(), void();
}
int main() {
iof();
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
ctime();
return 0;
}
Compilation message (stderr)
kyoto.cpp: In function 'void iof()':
kyoto.cpp:27:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
27 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |