-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCTRICK-2015849-src.c
66 lines (62 loc) · 947 Bytes
/
CTRICK-2015849-src.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <stdio.h>
int main()
{
int t, n, i, j, sqr, num, p, done;
int a[20000], rmq[142];
scanf("%d", &t);
while(t--)
{
scanf("%d", &n);
for(sqr=1; sqr*sqr<=n; sqr++)
;
sqr--;
for(i=0; i<n; i++)
a[i]=0;
p=(n/sqr)+(n%sqr!=0);
for(i=0; i<p; i++)
rmq[i]=0;
//Add base values of i, j here
j=-1;
for(num=1; num<=n; num++)
{
int next;
done=num;
j=(j+1)%n;
if(j/sqr==p-1)
next=n;
else
next=(j/sqr+1)*sqr;
for(; j<next && done; j++)
if(!a[j])
done--;
j=j%n;
i=j/sqr;
if(!done)
goto label;
for(; ; i=(i+1)%p)
{
if(i==p-1)
next=n-i*sqr;
else
next=sqr;
if(done>=next-rmq[i])
done-=next-rmq[i];
else
break;
}
j=i*sqr;
for(; done; j++)
if(!a[j])
done--;
label:
for(j=j%n; a[j]; j=(j+1)%n)
;
a[j]=num;
rmq[j/sqr]++;
}
for(i=0; i<n; i++)
printf("%d ", a[i]);
printf("\n");
}
return 0;
}