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<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
struct segtree
{
int tree[1<<18],base=1<<17;
void add(int place)
{
for(place+=base-1;place>=1;place>>=1)
tree[place]++;
}
int rangeSum(int s,int e)
{
int sum=0;
for(s+=base-1,e+=base-1;s<=e;s>>=1,e>>=1)
{
if(s&1) sum+=tree[s++];
if(!(e&1)) sum+=tree[e--];
}
return sum;
}
} p,q;
vector<int> cpr,z;
vector<pair<int,int>> v;
int a[100001];
int fin[100001];
int main()
{
int n,k;
scanf("%d %d",&n,&k);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
cpr.push_back(a[i]);
}
sort(cpr.begin(),cpr.end());
for(int i=1;i<=n;i++)
{
a[i]=lower_bound(cpr.begin(),cpr.end(),a[i])-cpr.begin()+1;
p.add(a[i]);
int c=p.rangeSum(a[i]+1,n);
if(k<=c) c=i-k;
else c=i-c;
v.push_back(make_pair(a[i],c));
}
sort(v.begin(),v.end());
for(int i=0;i<n;i++)
{
int s=v[i].second,e=n,ans;
while(s<=e)
{
int mid=(s+e)/2;
int res=q.rangeSum(s,mid);
if(res<mid-s+1)
ans=mid, e=mid-1;
else s=mid+1;
}
q.add(ans);
fin[ans]=v[i].first;
}
for(int i=1;i<=n;i++)
printf("%d ",cpr[fin[i]-1]);
return 0;
}
# | 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... |