Submission #1269654

#TimeUsernameProblemLanguageResultExecution timeMemory
1269654ducdevSafety (NOI18_safety)C++17
0 / 100
138 ms99184 KiB
// Author: 4uckd3v - Nguyen Cao Duc
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int MAX_N = 2e5;
const int MOD = 1e9 + 7;

int n, H;
int a[MAX_N + 5];
vector<int> compress;

namespace SUBTASK_4 {

};

namespace SUBTASK_127 {
    const int N = 5000;
    const int S = 5000;
    const int INF = 1e9;

    int dp[N + 5][S + 5];

    void Solve() {
        dp[0][0] = 0;
        for (int i = 1; i <= n; i++) {
            deque<int> dq;
            for (int j = 0; j <= H; j++) {
                while (!dq.empty() && dp[i - 1][dq.back()] <= dp[i - 1][j]) dq.pop_back();
                dq.push_back(j);
            };

            for (int j = 0; j <= S; j++) {
                while (!dq.empty() && dq.front() < max(0, j - H)) dq.pop_front();
                while (j + H <= S && !dq.empty() && dp[i - 1][dq.back()] >= dp[i - 1][j + H]) dq.pop_back();
                if (j + H <= S) dq.push_back(j + H);
                dp[i][j] = dp[i - 1][dq.front()] + abs(a[i] - j);
            };
        };

        int res = INF;
        for (int i = 0; i <= S; i++) res = min(res, dp[n][i]);

        cout << res << '\n';
    };
};  // namespace SUBTASK_127

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 >> H;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
    };

    SUBTASK_127::Solve();
};

Compilation message (stderr)

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