Submission #961656

#TimeUsernameProblemLanguageResultExecution timeMemory
961656vjudge1Safety (NOI18_safety)C++17
40 / 100
216 ms201608 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<int, int> pii; typedef pair<ll, ll> pll; #define ent "\n" const int maxn = 5e3 + 100; const ll INF = (ll)1e18 + 100; const int inf = 1e9 + 100; const int MOD = 1e9 + 7; const int maxl = 14; int n, h; int a[200010]; ll dp[maxn][maxn]; void test(){ cin >> n >> h; int mx = 0; for(int i = 1; i <= n ;i++){ cin >> a[i]; mx = max(mx, a[i]); } if(max(mx, n) <= 5000){ for(int x = 0; x <= 5000; x++){ dp[1][x] = abs(x - a[1]); } for(int i = 2; i <= n; i++){ deque<ll> d; for(int x = 0, l = 0, r = -1; x <= 5000; x++){ while(abs(r - x + 1) <= h && r < 5000){ r++; while(d.size() && d.back() > dp[i-1][r]){ d.pop_back(); } d.push_back(dp[i-1][r]); } while(abs(l - x) > h){ if(d.front() == dp[i-1][l]){ d.pop_front(); } l++; } dp[i][x] = d.front() + abs(x - a[i]); } } sort(dp[n], dp[n] + 5000 + 1); cout << dp[n][0]; } else if(!h){ sort(a + 1, a + n + 1); ll x = 0, y = 0; for(int i = 1; i <= n; i++){ y += a[i]; } ll ans = INF; for(int i = 1; i <= n; i++){ x += a[i]; ans = min(ans, 1ll * a[i] * i - x + y - x - 1ll * a[i] * (n - i)); } cout << ans; } } int main(){ ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t; t = 1; while(t--) test(); cout << ent; }
#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...