Submission #1264602

#TimeUsernameProblemLanguageResultExecution timeMemory
1264602ducdevStove (JOI18_stove)C++17
50 / 100
73 ms152900 KiB
// Author: 4uckd3v - Nguyen Cao Duc
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAX_N = 1e5;
const int MOD = 1e9 + 7;
const ll INF = 1e18;

template <class X, class Y>
bool minimize(X &x, const Y &y) {
    if (x <= y) return false;
    x = y;
    return true;
};

int n, k;
int a[MAX_N + 5];

namespace SUBTASK_12 {
    const int K = 5000;
    const int N = 5000;

    ll dp[K + 5][N + 5][2];

    void Solve() {
        for (int i = 0; i <= k; i++) {
            for (int j = 0; j <= n; j++) {
                dp[i][j][0] = dp[i][j][1] = INF;
            };
        };

        for (int i = 1; i <= n; i++) dp[1][i][1] = (a[i] + 1) - a[1];

        for (int i = 2; i <= k; i++) {
            for (int j = i; j <= n; j++) {
                dp[i][j][1] = min(dp[i - 1][j - 1][1] + 1, dp[i][j - 1][0] + (a[j] + 1) - a[j - 1]);
                dp[i][j][0] = min(dp[i - 1][j - 1][1], dp[i][j - 1][0] + a[j] - a[j - 1]);
            };
        };

        cout << dp[k][n][1] << '\n';
    };
};  // namespace SUBTASK_12

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    if (fopen("MAIN.INP", "r")) {
        freopen("MAIN.INP", "r", stdin);
        freopen("MAIN.OUT", "w", stdout);
    };

    cin >> n >> k;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    };

    SUBTASK_12::Solve();
    // SUBTASK_3::Solve();
};

Compilation message (stderr)

stove.cpp: In function 'int main()':
stove.cpp:51:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |         freopen("MAIN.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
stove.cpp:52:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   52 |         freopen("MAIN.OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...