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;
#define int long long
#define mod 1000000007
int n,k,ans=0;
int arr[5002],ARR[5002],taken[5002],mn;
vector <array<int,2>> v;
vector <int> curr;
map <int,int> indx;
bool check(){
for(int i=0;i<curr.size()-1;i++) if(arr[curr[i]]>arr[curr[i+1]]) return 0;
return 1;
}
void build2(int mid,bool b){
if(b==0) for(int i=v.size()-1;i>=v.size()-mid;i--) taken[v[i][1]]=0;
else for(int i=v.size()-1;i>=v.size()-mid;i--) taken[v[i][1]]=1;
}
void build1(){
cin>>n>>k;
mn=n;
for(int i=1;i<=n;i++){
cin>>arr[i];
ARR[i]=arr[i];
indx[arr[i]]=i;
taken[i]=1;
}
sort(ARR+1,ARR+n+1);
for(int i=1;i<=n;i++){
v.push_back({(int)ceil((double)i/k),indx[ARR[i]]*-1});
arr[indx[ARR[i]]]=(int)ceil((double)i/k);
}
sort(v.begin(),v.end());
for(int i=0;i<v.size();i++) v[i][1]*=-1;
return;
}
signed main()
{
build1();
for(int i=0;i<=n;i++){
int l=0,r=n-i,ans=1e9;
while(l<=r){
curr.clear();
int mid=(l+r)/2;
build2(mid,0);
for(int j=1;j<=n;j++) if(taken[j]==1) curr.push_back(j);
bool flag=check();
build2(mid,1);
if(flag==1){
ans=mid;
r=mid-1;
}
else l=mid+1;
}
mn=min(mn,i+ans);
taken[v[i][1]]=0;
}
cout<<mn<<endl;
return 0;
}
Compilation message (stderr)
studentsko.cpp: In function 'bool check()':
studentsko.cpp:11:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
11 | for(int i=0;i<curr.size()-1;i++) if(arr[curr[i]]>arr[curr[i+1]]) return 0;
| ~^~~~~~~~~~~~~~
studentsko.cpp: In function 'void build2(long long int, bool)':
studentsko.cpp:15:36: warning: comparison of integer expressions of different signedness: 'long long int' and 'long long unsigned int' [-Wsign-compare]
15 | if(b==0) for(int i=v.size()-1;i>=v.size()-mid;i--) taken[v[i][1]]=0;
| ~^~~~~~~~~~~~~~
studentsko.cpp:16:32: warning: comparison of integer expressions of different signedness: 'long long int' and 'long long unsigned int' [-Wsign-compare]
16 | else for(int i=v.size()-1;i>=v.size()-mid;i--) taken[v[i][1]]=1;
| ~^~~~~~~~~~~~~~
studentsko.cpp: In function 'void build1()':
studentsko.cpp:33:18: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
33 | for(int i=0;i<v.size();i++) v[i][1]*=-1;
| ~^~~~~~~~~
# | 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... |