#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pl = pair<ll,ll>;
using tl = tuple<ll,ll,ll>;
using ld = long double;
#define all(x) (x).begin(),(x).end()
#define sz(x) ll((x).size())
#define pb emplace_back
#define F first
#define S second
const ll
inf = (1ll<<62)
, N = 2e5 + 1
, K = 340
, mod = 1e9 + 7
;
ll n, h;
array<ll,N> a;
void solve1(){
ll l = 0, r = 1e9+1;
auto check = [&](ll mid){
ll t = 0;
for(ll i = 1;i<=n;i++)t+=abs(a[i]-mid);
return t;
};
while(l+3<r){
ll mid1 = l + (r-l)/3, mid2 = r - (r-l)/3;
if(check(mid1) > check(mid2))l=mid1;
else r = mid2;
}
ll ans = inf;
for(ll i = l;i<=r;i++)ans=min(ans,check(i));
cout << ans;
}
void solve2(){
vector<vector<ll>> dp(6000,vector<ll>(6000,inf));
fill(all(dp[0]),0);
for(ll i = 1;i<=n;i++){
multiset<ll> st;
for(ll j = 0;j<=h;j++)st.insert(dp[i-1][j]);
for(ll j = 0;j<=400;j++){
dp[i][j] = *st.begin() + abs(a[i] - j);
if(j>=h)st.erase(st.find(dp[i-1][j-h]));
if(j+h+1<=5000)st.insert(dp[i-1][j+h+1]);
}
}
cout << *min_element(all(dp[n]));
}
void solve(){
cin >> n >> h;
for(ll i = 1;i<=n;i++){
cin >> a[i];
}
if(h == 0){
solve1();
}
else if(n<=5000 && *max_element(all(a))<=5000){
solve2();
}
}
void precalc(){}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
ll t = 1;
precalc();
//cout << fixed << setprecision(10);
//cin >> t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
100 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Runtime error |
170 ms |
262148 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
1360 KB |
Output is correct |
2 |
Correct |
32 ms |
1672 KB |
Output is correct |
3 |
Correct |
34 ms |
1824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
100 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
100 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
100 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
100 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
100 ms |
262144 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |