Submission #549930

# Submission time Handle Problem Language Result Execution time Memory
549930 2022-04-16T21:04:32 Z NAHDI Skyline (IZhO11_skyline) C++14
100 / 100
204 ms 49396 KB
#include <bits/stdc++.h>
using namespace std;

using vi = vector<int>;
using vii = vector<vi>;
using vs = vector<string>;
using vss = vector<vs>;
using vb = vector<bool>;
using vbb  = vector<vb>;
using ii = pair<int, int>;
using vpi = vector<ii>;
using vpii = vector<vpi>;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using table = unordered_map<unsigned long, int>;
using pll = pair<ll,ll>;
using vpl = vector<pll>;
using vpll = vector<vpl>;

#define f first
#define s second

#define forn(i, n) for(int i = 0; i < n; i++)
#define fore(i, a, b) for(int i = a; i <= b; i++)
#define for1n(i, n) for(int i = 1; i <= n; i++)
#define rof(i, n) for(int i = n-1; i >= 0; i--)
#define rofe(i, a, b) for(int i = b; i >= a; i--)
#define all(x) x.begin(), x.end()
#define dsc(type) greater<type>

#define Flag cout << "Reached here.\n";
#define FASTIO ios::sync_with_stdio(0); cin.tie(0);

#define pb push_back
#define pbb pop_back
#define sz size
#define rsz resize
#define rsv reserve
#define ins insert

#define lb(a, val) lower_bound(all(a), val);
#define ub(a, val) upper_bound(all(a), val);

#define onec(x) __builtin_popcount(x)
#define end0(x) __builtin_clz(x)
#define beg0(x) __builtin_ctz(x)

#define MAX 100000000
#define MIN -MAX

#define mod 1000000007LL

#define clr(x, y) memset(x, y, sizeof(x))

template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }

int dx[] = {0, 1, -1, 0};
int dy[] = {1, 0, 0, -1};

#define N 302
#define H 202

int dp[H][H][N];
int h[N];
int n;

int memo(int last, int cur, int i) {
    if(last < 0 || cur < 0) return MAX;
    if(i == 1) {
        int mx = max(last, cur), mn = min(last, cur);
        return dp[last][cur][i] = (mn * 5) + ((mx-mn) * 3);
    }
    if(dp[last][cur][i] != -1) return dp[last][cur][i];

    dp[last][cur][i] = MAX;

    //take full
    int v = min({last, h[i-2], cur});
    if(v == cur) dp[last][cur][i] = min(dp[last][cur][i], memo(h[i-2]-v, last-v, i-1) + (7*v));

    dp[last][cur][i] = min({
        dp[last][cur][i],
        memo(last-1, cur-1, i) + 5,
        memo(last, cur-1, i) + 3,
    });
    return dp[last][cur][i];
}

void solve() {
    cin >> n;
    forn(i, n) cin >> h[i];

    if(n == 1) {    
        cout << (h[0] * 3) << '\n'; 
        return;
    }
    clr(dp, -1);
    cout << memo(h[n-2], h[n-1], n-1) << '\n';
}

int main() {
    FASTIO;
    solve();
}

/*
Let dp[last][cur][i] denote the ith element with current height and last height.
Obviously, we would want our cur to be h[i], and last to be h[i-1] in the final
answer. now, we select the minimum of the three following options: 

dp[last][cur-1][i] + 3, we are doing first operation.
dp[last-1][cur-1][i] + 5, we are doing second operation.

we don't have the third parameter. in this case, we must look at the total 
number of possible third operations we can do. Let v denote that.

Let's consider i-1. last-v will be our cur element, and maximum possible 
from i-2 will be h[i-2]-v. Thus, our candidate: 

dp[h[i-2]-v][last-v][i-1] + (7*v)
*/
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 18 ms 48512 KB Output is correct
3 Correct 22 ms 48456 KB Output is correct
4 Correct 20 ms 48520 KB Output is correct
5 Correct 18 ms 48448 KB Output is correct
6 Correct 18 ms 48496 KB Output is correct
7 Correct 18 ms 48460 KB Output is correct
8 Correct 18 ms 48468 KB Output is correct
9 Correct 21 ms 48480 KB Output is correct
10 Correct 22 ms 48576 KB Output is correct
11 Correct 36 ms 49048 KB Output is correct
12 Correct 25 ms 48596 KB Output is correct
13 Correct 38 ms 48980 KB Output is correct
14 Correct 52 ms 49192 KB Output is correct
15 Correct 125 ms 49396 KB Output is correct
16 Correct 116 ms 49164 KB Output is correct
17 Correct 178 ms 48588 KB Output is correct
18 Correct 204 ms 49060 KB Output is correct
19 Correct 157 ms 49364 KB Output is correct
20 Correct 171 ms 49048 KB Output is correct