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>
using namespace std;
int N,K;
struct pos{
int x,i;
bool operator <(const pos t) const{
if (x == t.x) return i < t.i;
return x < t.x;
}
}P[100005];
const int Z = 1<<17;
int it[2][Z*2],A[Z];
int sum(int *it, int y)
{
int s = 0;
int x = Z; y += Z;
while (x < y){
if (x & 1) s += it[x++];
if (~y & 1) s += it[y--];
x /= 2; y /= 2;
} if (x == y) s += it[x];
return s;
}
int trace(int *it, int x)
{
int s = 1;
while (s < Z){
s *= 2;
if (it[s] <= x){
x -= it[s];
s++;
}
}
return s - Z;
}
void del(int *it, int x)
{
x += Z;
while (x){
it[x]--;
x /= 2;
}
}
int main()
{
scanf ("%d %d",&N,&K);
for (int i=0;i<N;i++){
scanf ("%d",&P[i].x);
P[i].i = i;
}
sort(P,P+N);
for (int k=0;k<2;k++){
for (int i=0;i<N;i++) it[k][i+Z] = 1;
for (int j=Z-1;j>=1;j--) it[k][j] = it[k][j*2] + it[k][j*2+1];
}
for (int i=0;i<N;i++){
int u = max(0,sum(it[0],P[i].i)-K-1);
int x = trace(it[1],u);
A[x] = P[i].x;
del(it[0],P[i].i);
del(it[1],x);
}
for (int i=0;i<N;i++){
printf ("%d ",A[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... |