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<stdio.h>
#include<algorithm>
#include<set>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int,int> Pi;
#define X first
#define Y second
int p[100010], n, k, q[100010], per[100010], s[100010];
struct BIT{
int r[110];
void update(int x){for(int i=x+1;i<=n;i+=(i&-i))r[i]++;}
int read(int x){
int ret = 0;
for(int i=x+1;i;i-=(i&-i))ret += r[i];
return ret;
}
void clear(){for(int i=1;i<=n;i++)r[i] = 0;}
}bit;
bool comp(const int &a, const int &b){return p[a] < p[b];}
int main(){
scanf("%d%d",&n,&k);
for(int i=0;i<n;i++)scanf("%d",p+i);
for(int i=0;i<n;i++)per[i] = i;
sort(per, per+n, comp);
vector <int> v;
for(int i=0;i<n;i++){
if(i > 0 && p[per[i]] != p[per[i-1]]){
for(int j=0;j<(int)v.size();j++)bit.update(per[v[j]]);
v.clear();
}
s[i] = max(per[i] - bit.read(per[i]) - k, 0);
v.push_back(per[i]);
}
bit.clear();
for(int i=0;i<n;i++){
int low=0, high=n-1, cor=0;
while(low<=high){
int mid = (low+high) / 2;
if(s[i] <= mid - bit.read(mid))cor = mid, high = mid - 1;
else low = mid + 1;
}
q[cor] = p[per[i]];
bit.update(cor);
}
for(int i=0;i<n;i++)printf("%d ",q[i]);
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... |