Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- struct cmp {
- bool operator()(pair<int,int> const& a, pair<int,int> const& b)
- {
- if((a.second-a.first)!=(b.second-b.first))return (a.second-a.first)<(b.second-b.first);
- if(a.first!=b.first)return a.first>b.first;
- return true;
- }
- };
- vector<int> findArray(int N){
- vector<int> A(N);
- priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> pq;
- pq.push({0,N-1});
- // int count = 1;
- while(!pq.empty()){
- auto temp = pq.top();
- pq.pop();
- int len = temp.second-temp.first+1;
- if(len&1){
- int pivot = (temp.first + temp.second)/2;
- A[pivot] = len;
- if(pivot-temp.first){
- pq.push({temp.first,pivot-1});
- }
- if(temp.second-pivot){
- pq.push({pivot+1,temp.second});
- }
- }
- else{
- int pivot = (temp.first + temp.second-1)/2;
- A[pivot] = len;
- if(pivot-temp.first){
- pq.push({temp.first,pivot-1});
- }
- if(temp.second-pivot){
- pq.push({pivot+1,temp.second});
- }
- }
- }
- return A;
- }
- int main(){
- int N;
- cin>>N;
- vector<int> A = findArray(N);
- for(int i=0;i<N;i++){
- cout<<A[i]<<" ";
- }
- cout<<endl;
- }
Add Comment
Please, Sign In to add comment