Submission #335372

#TimeUsernameProblemLanguageResultExecution timeMemory
335372ncduy0303Safety (NOI18_safety)C++17
13 / 100
2079 ms18540 KiB
#include <bits/stdc++.h> using namespace std; #define ar array #define ll long long const int MAX_N = 2e5 + 1; const int MOD = 1e9 + 7; const int INF = 1e9; const ll LINF = 1e18; void solve() { int n, m; cin >> n >> m; vector<int> a(n); int mx = 0; for (int &x : a) cin >> x, mx = max(mx, x); m = min(mx, m); ll dp[n + 1][mx + 1]; memset(dp, 0x3f, sizeof dp); for (int i = 0; i <= mx; i++) dp[0][i] = abs(i - a[0]); // Naive O(n*m*mx) for (int i = 1; i < n; i++) { for (int j = 0; j <= mx; j++) { for (int k = max(0, j - m); k <= min(mx, j + m); k++) { dp[i][j] = min(dp[i][j], dp[i - 1][k] + abs(j - a[i])); // dp[i][j] = min{...} + constant } } } ll ans = LINF; for (int i = 0; i <= mx; i++) ans = min(ans, dp[n - 1][i]); cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); // freopen("input.txt", "r", stdin); // freopen("output.txt", "w", stdout); int tc = 1; // cin >> tc; for (int t = 1; t <= tc; t++) { // cout << "Case #" << t << ": "; solve(); } }
#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...