Submission #830717

#TimeUsernameProblemLanguageResultExecution timeMemory
830717vjudge1Safety (NOI18_safety)C++17
40 / 100
352 ms196212 KiB
#include<bits/stdc++.h> #define ll long long #define fi first #define se second using namespace std; const int nmax = 200002; ll S[nmax]; ll dp[5005][5005]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); ll n,h; deque<pair<ll,ll>>dq; cin >> n >> h; for(int i=1;i<=n;i++)cin >> S[i]; if(h==0) { sort(S+1, S+n+1); ll jwb = 0; for(int i=1; i<=n; i++) { jwb+=abs(S[i]-S[n/2+1]); } cout << jwb << endl; return 0; } for(int i=0;i<=5000;i++)dp[1][i] = abs(S[1] - i); for(int i=2;i<=n;i++){ dq.clear(); for(int j=0;j<=min(5000ll, h);j++){ while(!dq.empty() && dq.back().fi > dp[i-1][j])dq.pop_back(); dq.push_back({dp[i-1][j], j}); } for(int j=0;j<=5000;j++){ if(!dq.empty() && dq.front().se < j - h)dq.pop_front(); pair<ll,ll> tmp = dq.front(); dp[i][j] = tmp.fi + abs(S[i] - j); if(j + h + 1 <= 5000){ while(!dq.empty() && dq.back().fi > dp[i-1][j + h + 1])dq.pop_back(); dq.push_back({dp[i-1][j+h+1], j+h+1}); } } } ll ans = 1e18; for(int i=0;i<=5000;i++)ans = min(ans, dp[n][i]); cout << ans; return 0; }
#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...