Lena Sort


Problem Statement :


Lena developed a sorting algorithm described by the following pseudocode:

lena_sort(array nums) {
    if (nums.size <= 1) {
        return nums;
    }
    pivot = nums[0];
    array less;
    array more;
    for (i = 1; i < nums.size; ++i) {
    	// Comparison
        if (nums[i] < pivot) {
            less.append(nums[i]);
        }
        else {
            more.append(nums[i]);
        }
    }
    sorted_less = lena_sort(less);
    sorted_more = lena_sort(more);
    ans = sorted_less + pivot + sorted_more;
    
    return ans;
}
We consider a comparison to be any time some nums[i] is compared with pivot.

You must solve q queries where each query i consists of some len(i) and ci. For each query, construct an array of len(i) distinct elements in the inclusive range between 1 and 10^9 that will be sorted by lena_sort in exactly ci comparisons, then print each respective element of the unsorted array as a single line of leni space-separated integers; if no such array exists, print -1 instead.

Input Format

The first line contains a single integer denoting q (the number of queries).
Each line i of the q subsequent lines contains two space-separated integers describing the respective values of len(i) (the length of the array) and ci (the number of comparisons) for query i.

Constraints

1 <= q <= 10^5
1 <= len(i) <= 10^5
0 <= ci <= 10^9
1 <=  the sum of len(i) over all queries <= 10^6

Output Format

Print the answer to each query on a new line. For each query i, print len(i) space-separated integers describing each respective element in an unsorted array that Lena's algorithm will sort in exactly ci comparisons; if no such array exists, print -1 instead.



Solution :



title-img


                            Solution in C :

In C++ :






#include <bits/stdc++.h>

using namespace std;

#ifdef WIN32
	#define I64 "%I64d"
#else
	#define I64 "%lld"
#endif

typedef long long ll;

#define f first
#define s second
#define mp make_pair
#define pb push_back
#define all(s) s.begin(), s.end()
#define sz(s) (int(s.size()))
#define fname "a"
#define MAXN 200002

int n, k;
int a[MAXN];
ll d[MAXN];
ll f[MAXN];

void go(int l, int r, int k, int ff) {
	if (l > r) return;
	if (l == r) {
		a[l] = ff;
		return;
	}
	int n = r - l + 1;
	k -= n - 1;
	for (int i = 0; i < n; ++i) {
		if (d[i] + d[n - 1 - i] <= k && f[i] + f[n - 1 - i] >= k)
                   {
			a[l] = ff + i;
			int k1 = d[i];
			int k2 = k - d[i];
			if (k2 > f[n - 1 - i]) {
				k1 += k2 - f[n - 1 - i];
				k2 = f[n - 1 - i];
			}
			go(l + 1, l + i, k1, ff);
			go(l + i + 1, r, k2, ff + i + 1);
			return;
		}
	}
}

inline void solve()
{
	scanf("%d%d", &n, &k);
	if (k < d[n] || k > f[n]) {
		puts("-1");
		return;
	}
	go(0, n - 1, k, 0);
	for (int i = 0; i < n; ++i)
		printf("%d%c", a[i] + 1, " \n"[i + 1 == n]);
}

int main()
{
	#ifdef LOCAL
	freopen(fname".in", "r", stdin);
	freopen(fname".out", "w", stdout);
	#endif

	for (int i = 2; i < MAXN; ++i) {
		d[i] = i - 1 + d[i / 2] + d[(i - 1) / 2];
		f[i] = 1LL * (i - 1) * i / 2;
	}

	int tt;
	scanf("%d", &tt);
	for (int t = 0; t < tt; ++t)
		solve();

	return 0;
}





In Java :





import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        long[] mins = new long[100001];
        long[] maxes = new long[100001];
        mins[2] = 1;
        maxes[2] = 1;
        for (int i = 3; i <= 100000; i++) {
            mins[i] = i-1+mins[(i-1)/2]+mins[(i-1)-(i-1)/2];
            maxes[i] = maxes[i-1]+i-1;
        }
        Scanner in = new Scanner(System.in);
        int q = in.nextInt();
        for(int a0 = 0; a0 < q; a0++){
            int len = in.nextInt();
            int c = in.nextInt();
            if (maxes[len]<c||mins[len]>c) {
                System.out.println(-1);
                continue;
            }
            System.out.println(portion(len, c, 1, mins, maxes, new StringBuilder()));
        }
    }
    
    public static StringBuilder portion(int len, long c, int offset, long[] mins, long[] maxes, StringBuilder ans) {
        if (len==0) {
            return ans;
        }
        if (len==1) {
            ans.append(offset+" ");
            return ans;
        }
        int pivot = 0;
        c -= len-1;
        while (mins[pivot]+mins[len-pivot-1]>c||maxes[pivot]+maxes[len-pivot-1]<c)
            pivot++;
        long newc = mins[pivot];
        while (mins[len-pivot-1]>c-newc||maxes[len-pivot-1]<c-newc)
            newc++;
        ans.append((pivot+offset)+" ");
        portion(pivot, newc, offset, mins, maxes, ans);
        portion(len-pivot-1, c-newc, offset+pivot+1, mins, maxes, ans);
        return ans;
    }
}
                        








View More Similar Problems

Contacts

We're going to make our own Contacts application! The application must perform two types of operations: 1 . add name, where name is a string denoting a contact name. This must store name as a new contact in the application. find partial, where partial is a string denoting a partial name to search the application for. It must count the number of contacts starting partial with and print the co

View Solution →

No Prefix Set

There is a given list of strings where each string contains only lowercase letters from a - j, inclusive. The set of strings is said to be a GOOD SET if no string is a prefix of another string. In this case, print GOOD SET. Otherwise, print BAD SET on the first line followed by the string being checked. Note If two strings are identical, they are prefixes of each other. Function Descriptio

View Solution →

Cube Summation

You are given a 3-D Matrix in which each block contains 0 initially. The first block is defined by the coordinate (1,1,1) and the last block is defined by the coordinate (N,N,N). There are two types of queries. UPDATE x y z W updates the value of block (x,y,z) to W. QUERY x1 y1 z1 x2 y2 z2 calculates the sum of the value of blocks whose x coordinate is between x1 and x2 (inclusive), y coor

View Solution →

Direct Connections

Enter-View ( EV ) is a linear, street-like country. By linear, we mean all the cities of the country are placed on a single straight line - the x -axis. Thus every city's position can be defined by a single coordinate, xi, the distance from the left borderline of the country. You can treat all cities as single points. Unfortunately, the dictator of telecommunication of EV (Mr. S. Treat Jr.) do

View Solution →

Subsequence Weighting

A subsequence of a sequence is a sequence which is obtained by deleting zero or more elements from the sequence. You are given a sequence A in which every element is a pair of integers i.e A = [(a1, w1), (a2, w2),..., (aN, wN)]. For a subseqence B = [(b1, v1), (b2, v2), ...., (bM, vM)] of the given sequence : We call it increasing if for every i (1 <= i < M ) , bi < bi+1. Weight(B) =

View Solution →

Kindergarten Adventures

Meera teaches a class of n students, and every day in her classroom is an adventure. Today is drawing day! The students are sitting around a round table, and they are numbered from 1 to n in the clockwise direction. This means that the students are numbered 1, 2, 3, . . . , n-1, n, and students 1 and n are sitting next to each other. After letting the students draw for a certain period of ti

View Solution →