Monk and Tasks

QUESTION

Monk A loves to complete all his tasks just before the deadlines for introducing unwanted thrill in his life. But, there is another Monk D who hates this habit of Monk A and thinks it’s risky.\n\nTo test Monk A, Monk D provided him tasks for N days in the form of an array Array, where the elements of the array represent the number of tasks.\n\nThe number of tasks performed by Monk A on the ith day is the number of ones in the binary representation of Arrayi.\n\nMonk A is fed up of Monk D, so to irritate him even more, he decides to print the tasks provided in non-decreasing order of the tasks performed by him on each day. Help him out!\n\nInput:\nThe first line of input contains an integer T, where T is the number of test cases.\nThe first line of each test case contains N, where N is the number of days.\nThe second line of each test case contains Array array having N elements, where Arrayi represents the number of tasks provided by Monk D to Monk A on ith day.\n\nOutput:\nPrint all the tasks provided to Monk A in the non-decreasing order of number of tasks performed by him.\n\nConstraints:\n1 <= T <= 100\n1 <= N <= 10^5\n1 <= Arrayi <= 10^18\n\nNote:\nIf two numbers have the same number of ones (set bits), print the one which came first in the input first, and then the other one, as in the input.

ANSWER

#include<bits/stdc++.h>
using namespace std;
 
#define rep(j,n) for(int i=j;i<n;i++)
#define rep2(j,n) for(int i=j;i<n;i--)
#define pb push_back
#define mp make_pair
#define ll long long int
#define pii pair<int,int> 
#define gc getchar_unlocked
#define mod 1000000007
void read(int &x){
    x=0;
    register char c=gc();
    for(;c<'0' || c>'9';c=gc());
    	for(;c>='0' && c<='9';c=gc())
          	x=(x<<3)+(x<<1)+(c-'0');
}
void write(int x){
 
    register char buffor[35];
    register int i=0;
    do{
        buffor[i++]=(x%10)+'0';
        x/=10;
    } while(x);
    i--;
    while(i>=0) putchar_unlocked(buffor[i--]);
    putchar_unlocked('\n');
}
 
vector <int> v[64];
int main(){
	int t,n,a,c,j;
	read(t);
	rep(0,t){
		read(n);
		rep(0,n){
			c=0;
			read(a);
			j=a;
			while(a){
				if(a&1)
				c++;
				a=a>>1;
			}
			v[c].pb(j);
		}
		rep(0,64){
			for(int k=0;k<v[i].size();k++){
				cout<<v[i][k]<<" ";
			}
		}
		cout<<endl;
		rep(0,64){
			v[i].clear();
		}
	}
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.