#include<iostream>#include<vector>// Merge sort and count inversions.longlongmerge_sort(std::vector<int>&nums,intb,inte){// Return 0 when length <= 1.if(e-b<=1)return0;longlongres=0;intm=(b+e)/2;// Merge_sort two halves.res+=merge_sort(nums,b,m);res+=merge_sort(nums,m,e);// Temporary vector to store sorted array.std::vector<int>tmp(e-b);inti=b,j=m,k=0;while(i<m&&j<e){if(nums[j]<nums[i]){tmp[k]=nums[j++];// In this case, all elements in [i,m) are larger than element j.res+=m-i;}else{tmp[k]=nums[i++];}++k;}// Deal with remaining elements.for(;i<m;++i,++k){tmp[k]=nums[i];}for(;j<e;++j,++k){tmp[k]=nums[j];}// Copy back to original vector.for(i=b,k=0;i<e;++i,++k){nums[i]=tmp[k];}returnres;}intmain(){intn;std::cin>>n;std::vector<int>nums(n);for(int&num:nums){std::cin>>num;}std::cout<<merge_sort(nums,0,n);return0;}
#include<algorithm>#include<iostream>#include<unordered_map>#include<vector>// A simple BIT implementation.classBIT{intn;std::vector<int>su;public:BIT(intn):n(n),su(n+1){}// Add v to the x-th number.voidadd(intx,intv){for(;x<=n;x+=x&(-x)){su[x]+=v;}}// Get the cumulative sum till the x-th number.intquery(intx){intres=0;for(;x;x&=x-1){res+=su[x];}returnres;}};// Count inversions.longlongsolve(conststd::vector<int>&nums){// Discretization.std::vector<int>sorted(nums);std::sort(sorted.begin(),sorted.end());sorted.erase(std::unique(sorted.begin(),sorted.end()),sorted.end());std::unordered_map<int,int>ids;intm=sorted.size();for(inti=0;i<m;++i){// Reverse the order.// Now a smaller id means a larger element.ids[sorted[i]]=m-i;}// Main part.BITbit(m);longlongres=0;for(intnum:nums){intid=ids[num];// Get inversion pair (i,j) with j the current element.// Namely, count the number of elements larger than// the current one but located before it.res+=bit.query(id-1);// Insert the current element to the BIT.bit.add(id,1);}returnres;}intmain(){intn;std::cin>>n;std::vector<int>nums(n);for(int&num:nums){std::cin>>num;}std::cout<<solve(nums);return0;}
#include<iostream>#include<vector>// A simple BIT implementation.classBIT{intn;std::vector<int>su;public:BIT(intn):n(n),su(n+1){}// Add v to the x-th number.voidadd(intx,intv){for(;x<=n;x+=x&(-x)){su[x]+=v;}}// Get the cumulative sum till the x-th number.intquery(intx){intres=0;for(;x;x&=x-1){res+=su[x];}returnres;}};// Get the rank of a permutation of 1~n.longlongfind_rank(conststd::vector<int>&nums){intn=nums.size();BITbit(n);longlongfac=1;longlongres=0;// Reverse iteration.for(inti=n-1;i>=0;--i){// Count the number of elements smaller than the current one.res+=bit.query(nums[i]-1)*fac;// Insert the current element into the BIT.bit.add(nums[i],1);// Update the factorial.fac*=n-i;}returnres+1;}intmain(){intn;std::cin>>n;std::vector<int>nums(n);for(int&num:nums){std::cin>>num;}std::cout<<find_rank(nums);return0;}
#include<cmath>#include<iostream>#include<vector>// A simple BIT implementation.classBIT{intn;std::vector<int>su;public:BIT(intn):n(n),su(n+1){}// Fill the BIT with one.voidfill(){for(intx=1;x<=n;++x){su[x]+=x&(-x);}}// Add v to the x-th number.voidadd(intx,intv){for(;x<=n;x+=x&(-x)){su[x]+=v;}}// Get the k-th smallest element.intfind_kth(intk){intps=0,x=0;for(inti=log2(n);i>=0;--i){x+=1<<i;if(x>=n||ps+su[x]>=k){x-=1<<i;}else{ps+=su[x];}}returnx+1;}};// Find the k-th permutation of 1~n.std::vector<int>find_permutation(intn,longlongk){--k;// Expand rank to Lehmer code.std::vector<int>lehmer(n);for(inti=1;i<=n;++i){lehmer[n-i]=k%i;k/=i;}BITbit(n);// Set all values in BIT to one.bit.fill();std::vector<int>res(n);for(inti=0;i<n;++i){// Find the lehmer[i]-th smallest unused element.res[i]=bit.find_kth(lehmer[i]+1);// Remove it from the BIT.bit.add(res[i],-1);}returnres;}intmain(){intn;longlongk;std::cin>>n>>k;autores=find_permutation(n,k);for(intnum:res){std::cout<<num<<' ';}return0;}