제출 #580298

#제출 시각아이디문제언어결과실행 시간메모리
580298balbitSightseeing in Kyoto (JOI22_kyoto)C++14
40 / 100
45 ms34720 KiB
#include <bits/stdc++.h>
using namespace std;

#define int ll

#define ll long long
#define pii pair<ll, ll>
#define f first
#define s second

#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)

#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define pb push_back

#define FOR(i,a,b) for (int i = a; i<b; ++i)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) for (int i = n-1; i>=0; --i)
#define REP1(i,n) FOR(i,1,n+1)

#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT

const int maxn = 2e3+5;
const ll inf = 0x3f3f3f3f3f3f3f3f;

int xpos[maxn], ypos[maxn];
int xv[maxn], yv[maxn];



ll dp[maxn][maxn];

vector<pii> clean(vector<int> raw) {
    int n = SZ(raw);
    vector<int> prefmin(n+2, inf), sufmin(n+2, inf);

    REP(i, n) {
        prefmin[i+1] = min(prefmin[i], raw[i]);
    }

    for (int i = n-2; i>=0; --i) {
        sufmin[i] = min(sufmin[i+1], raw[i+1]);
    }

    vector<pii> re;

    REP(i,n) {
        if (raw[i] >= prefmin[i] && raw[i] >= sufmin[i]) {
            // useless
            continue;
        }
        re.pb({raw[i], i});
    }

    return re;
}

signed main(){
    ios::sync_with_stdio(0), cin.tie(0);
    bug(1,2);

    int H,W; cin>>H>>W;
    int n,m;
    {
        vector<int> tmp(H);
        REP(i,H) cin>>tmp[i];
        vector<pii> gx = clean(tmp);
        n = SZ(gx);
        REP(i, n) {
            xpos[i] = gx[i].s, xv[i] = gx[i].f;
        }
    }
    {
        vector<int> tmp(W);
        REP(i,W) cin>>tmp[i];
        vector<pii> gy = clean(tmp);
        m = SZ(gy);
        REP(i, m) {
            ypos[i] = gy[i].s, yv[i] = gy[i].f;
        }
    }

    bug(n,m);

    memset(dp, 0x3f, sizeof dp);
    dp[0][0] = 0;
    REP(i,n) {
        REP(j,m) {
            if (i) MN(dp[i][j], dp[i-1][j] + yv[j] * (xpos[i] - xpos[i-1]));
            if (j) MN(dp[i][j], dp[i][j-1] + xv[i] * (ypos[j] - ypos[j-1]));
        }
    }

    cout<<dp[n-1][m-1]<<endl;

}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...