#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ldb long double
#define db double
#define fin(x) freopen(x,"r",stdin)
#define fout(x) freopen(x,"w",stdout)
#define fo(i,l,r) for(int i=(l);i<=(r);i++)
#define foi(i,l,r) for(int i=(l);i>=(r);i--)
#define el cout<<'\n';
#define cel cerr<<'\n';
#define all(x) x.begin(),x.end()
#define ii pair<int,int>
#define iii pair<int,ii>
#define gb(x,i) (((x)>>(i))&1)
#define mask(i) (1LL<<(i))
using namespace std;
const int N=2e5+5;
const int bl=60;
const ll base=1e9+7;
const ll inf=1e9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll Rand(ll l,ll r)
{
return l+(1ll*rng()*rng()%(r-l+1)+(r-l+1))%(r-l+1);
}
template<class X,class Y>bool maximize(X &a,Y b)
{
if(a<b) return a=b,true;
return false;
}
template<class X,class Y>bool minimize(X &a, Y b)
{
if(a>b) return a=b,true;
return false;
}
void add(auto &a,auto b)
{
a+=b;
if(a>=base) a-=base;
if(a<0) a+=base;
}
int n,diff,h[N];
namespace sub12567
{
ll dp[N],f[N];
deque<int>dq;
void solve()
{
fo(i,1,n)
{
fo(j,0,5e3) f[j]=1e18;
dq.clear();
fo(j,0,5e3)
{
while(dq.size() and dp[dq.back()]>=dp[j]) dq.pop_back();
dq.pb(j);
while(j-dq.front()>diff) dq.pop_front();
minimize(f[j],dp[dq.front()]);
}
dq.clear();
foi(j,5e3,0)
{
while(dq.size() and dp[dq.back()]>=dp[j]) dq.pop_back();
dq.pb(j);
while(dq.front()-j>diff) dq.pop_front();
minimize(f[j],dp[dq.front()]),f[j]+=abs(h[i]-j);
}
fo(j,0,5e3) dp[j]=f[j];
}
ll ans=1e18;
fo(i,0,5e3) minimize(ans,dp[i]);
cout<<ans;
}
}
int main()
{
#define task "o"
if(fopen(task".inp","r"))
{
fin(task".inp");
//fout(task".out");
}
srand(time(NULL));
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>diff;
fo(i,1,n) cin>>h[i];
if(n<=5e3)
{
sub12567::solve();
return 0;
}
if(!diff)
{
sort(h+1,h+n+1);
int x=h[(n+1)/2];
ll ans=0;
fo(i,1,n) ans+=abs(h[i]-x);
return cout<<ans,0;
}
}
Compilation message
safety.cpp:40:10: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
40 | void add(auto &a,auto b)
| ^~~~
safety.cpp:40:18: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
40 | void add(auto &a,auto b)
| ^~~~
safety.cpp: In function 'int main()':
safety.cpp:9:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | #define fin(x) freopen(x,"r",stdin)
| ~~~~~~~^~~~~~~~~~~~~
safety.cpp:84:9: note: in expansion of macro 'fin'
84 | fin(task".inp");
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
2 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
856 KB |
Output is correct |
2 |
Correct |
26 ms |
1116 KB |
Output is correct |
3 |
Correct |
27 ms |
1180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
28 ms |
556 KB |
Output is correct |
14 |
Correct |
27 ms |
348 KB |
Output is correct |
15 |
Correct |
25 ms |
348 KB |
Output is correct |
16 |
Correct |
28 ms |
348 KB |
Output is correct |
17 |
Correct |
27 ms |
348 KB |
Output is correct |
18 |
Correct |
21 ms |
348 KB |
Output is correct |
19 |
Correct |
27 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
28 ms |
556 KB |
Output is correct |
14 |
Correct |
27 ms |
348 KB |
Output is correct |
15 |
Correct |
25 ms |
348 KB |
Output is correct |
16 |
Correct |
28 ms |
348 KB |
Output is correct |
17 |
Correct |
27 ms |
348 KB |
Output is correct |
18 |
Correct |
21 ms |
348 KB |
Output is correct |
19 |
Correct |
27 ms |
348 KB |
Output is correct |
20 |
Correct |
27 ms |
348 KB |
Output is correct |
21 |
Correct |
27 ms |
540 KB |
Output is correct |
22 |
Correct |
24 ms |
344 KB |
Output is correct |
23 |
Correct |
20 ms |
344 KB |
Output is correct |
24 |
Correct |
26 ms |
348 KB |
Output is correct |
25 |
Correct |
21 ms |
344 KB |
Output is correct |
26 |
Correct |
28 ms |
572 KB |
Output is correct |
27 |
Correct |
30 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
28 ms |
556 KB |
Output is correct |
14 |
Correct |
27 ms |
348 KB |
Output is correct |
15 |
Correct |
25 ms |
348 KB |
Output is correct |
16 |
Correct |
28 ms |
348 KB |
Output is correct |
17 |
Correct |
27 ms |
348 KB |
Output is correct |
18 |
Correct |
21 ms |
348 KB |
Output is correct |
19 |
Correct |
27 ms |
348 KB |
Output is correct |
20 |
Correct |
27 ms |
348 KB |
Output is correct |
21 |
Correct |
27 ms |
540 KB |
Output is correct |
22 |
Correct |
24 ms |
344 KB |
Output is correct |
23 |
Correct |
20 ms |
344 KB |
Output is correct |
24 |
Correct |
26 ms |
348 KB |
Output is correct |
25 |
Correct |
21 ms |
344 KB |
Output is correct |
26 |
Correct |
28 ms |
572 KB |
Output is correct |
27 |
Correct |
30 ms |
348 KB |
Output is correct |
28 |
Correct |
273 ms |
548 KB |
Output is correct |
29 |
Correct |
230 ms |
552 KB |
Output is correct |
30 |
Correct |
262 ms |
344 KB |
Output is correct |
31 |
Correct |
216 ms |
560 KB |
Output is correct |
32 |
Correct |
226 ms |
592 KB |
Output is correct |
33 |
Correct |
275 ms |
344 KB |
Output is correct |
34 |
Correct |
248 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
344 KB |
Output is correct |
9 |
Correct |
2 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |