This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int nx=5e3+5;
int n, k, v[nx], t, l[nx], r[nx], g, res=INT_MAX, dp[nx];
map<int, int> mp;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>k;
g=n/k;
for (int i=1; i<=n; i++) cin>>v[i], mp[-v[i]]=0, l[i]=INT_MAX, r[i]=0;
for (auto &[x, y]:mp) y=(t/k)+1, t++;
for (int i=1; i<=n; i++) v[i]=mp[-v[i]], l[v[i]]=min(l[v[i]], i), r[v[i]]=max(r[v[i]], i);
for (int i=1; i<=g; i++)
{
dp[i]=i;
if (i>1&&r[i]<l[i-1]) dp[i]=dp[i-1];
//cout<<"dp "<<i<<' '<<dp[i]<<'\n';
int cur=(n-(i-dp[i]+1)*k);
for (int j=r[dp[i]]+1; j<=n; j++) if (v[j]==dp[i]-1) cur--;
for (int j=1; j<l[i]; j++) if (v[j]==i+1) cur--;
res=min(res, cur);
}
cout<<res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |