#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
852 KB |
Output is correct |
3 |
Correct |
1 ms |
852 KB |
Output is correct |
4 |
Correct |
1 ms |
852 KB |
Output is correct |
5 |
Correct |
1 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
1280 KB |
Output is correct |
2 |
Correct |
27 ms |
3588 KB |
Output is correct |
3 |
Correct |
39 ms |
3632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
36 ms |
19872 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
33 ms |
19392 KB |
Output is correct |
16 |
Correct |
30 ms |
19888 KB |
Output is correct |
17 |
Correct |
27 ms |
17620 KB |
Output is correct |
18 |
Correct |
23 ms |
14676 KB |
Output is correct |
19 |
Correct |
28 ms |
19032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
36 ms |
19872 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
33 ms |
19392 KB |
Output is correct |
16 |
Correct |
30 ms |
19888 KB |
Output is correct |
17 |
Correct |
27 ms |
17620 KB |
Output is correct |
18 |
Correct |
23 ms |
14676 KB |
Output is correct |
19 |
Correct |
28 ms |
19032 KB |
Output is correct |
20 |
Correct |
31 ms |
18528 KB |
Output is correct |
21 |
Correct |
33 ms |
19924 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
26 ms |
15348 KB |
Output is correct |
24 |
Correct |
31 ms |
18508 KB |
Output is correct |
25 |
Correct |
27 ms |
15620 KB |
Output is correct |
26 |
Correct |
33 ms |
19900 KB |
Output is correct |
27 |
Correct |
31 ms |
18508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Correct |
36 ms |
19872 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
33 ms |
19392 KB |
Output is correct |
16 |
Correct |
30 ms |
19888 KB |
Output is correct |
17 |
Correct |
27 ms |
17620 KB |
Output is correct |
18 |
Correct |
23 ms |
14676 KB |
Output is correct |
19 |
Correct |
28 ms |
19032 KB |
Output is correct |
20 |
Correct |
31 ms |
18528 KB |
Output is correct |
21 |
Correct |
33 ms |
19924 KB |
Output is correct |
22 |
Correct |
0 ms |
340 KB |
Output is correct |
23 |
Correct |
26 ms |
15348 KB |
Output is correct |
24 |
Correct |
31 ms |
18508 KB |
Output is correct |
25 |
Correct |
27 ms |
15620 KB |
Output is correct |
26 |
Correct |
33 ms |
19900 KB |
Output is correct |
27 |
Correct |
31 ms |
18508 KB |
Output is correct |
28 |
Correct |
307 ms |
196212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
321 ms |
192984 KB |
Output is correct |
31 |
Correct |
260 ms |
159832 KB |
Output is correct |
32 |
Correct |
256 ms |
161356 KB |
Output is correct |
33 |
Correct |
352 ms |
196176 KB |
Output is correct |
34 |
Correct |
323 ms |
192128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
596 KB |
Output is correct |
2 |
Correct |
1 ms |
724 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
724 KB |
Output is correct |
5 |
Correct |
1 ms |
724 KB |
Output is correct |
6 |
Correct |
1 ms |
596 KB |
Output is correct |
7 |
Correct |
1 ms |
724 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
852 KB |
Output is correct |
10 |
Correct |
1 ms |
852 KB |
Output is correct |
11 |
Correct |
1 ms |
852 KB |
Output is correct |
12 |
Correct |
1 ms |
852 KB |
Output is correct |
13 |
Incorrect |
1 ms |
596 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |