-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstructingtheArray.cpp
More file actions
50 lines (44 loc) · 933 Bytes
/
ConstructingtheArray.cpp
File metadata and controls
50 lines (44 loc) · 933 Bytes
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
#include<bits/stdc++.h>
using namespace std;
#define optimize() ios_base:: sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mx = 2e5+123;
int a[mx];
int main(){
optimize();
int t;
cin>>t;
while (t--)
{
int n;
cin>>n;
priority_queue<pair<int,int> >p;
p.push({n,-1});
int cnt=1;
while (!p.empty())
{
int l=p.top().second*-1;
int len=p.top().first;
int r=l+len-1;
p.pop();
int mid;
if(len%2) mid=(l+r)/2;
else mid=(l+r-1)/2;
a[mid]=cnt;
cnt++;
int l1,r1,l2,r2;
l1=l;
r1=mid-1;
l2=mid+1;
r2=r;
if(l1<=r1) p.push({r1-l1+1, -l1});
if(l2<=r2) p.push({r2-l2+1, -l2});
}
for (int i = 1; i <=n; i++) cout<<a[i]<<" ";
cout<<endl;
}
return 0;
}
/*
Author : SALAH
"HARD WORK CAN CHANGE LUCK"
*/