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 :
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
Find the Running Median
The median of a set of integers is the midpoint value of the data set for which an equal number of integers are less than and greater than the value. To find the median, you must first sort your set of integers in non-decreasing order, then: If your set contains an odd number of elements, the median is the middle element of the sorted sample. In the sorted set { 1, 2, 3 } , 2 is the median.
View Solution →Minimum Average Waiting Time
Tieu owns a pizza restaurant and he manages it in his own way. While in a normal restaurant, a customer is served by following the first-come, first-served rule, Tieu simply minimizes the average waiting time of his customers. So he gets to decide who is served first, regardless of how sooner or later a person comes. Different kinds of pizzas take different amounts of time to cook. Also, once h
View Solution →Merging Communities
People connect with each other in a social network. A connection between Person I and Person J is represented as . When two persons belonging to different communities connect, the net effect is the merger of both communities which I and J belongs to. At the beginning, there are N people representing N communities. Suppose person 1 and 2 connected and later 2 and 3 connected, then ,1 , 2 and 3 w
View Solution →Components in a graph
There are 2 * N nodes in an undirected graph, and a number of edges connecting some nodes. In each edge, the first value will be between 1 and N, inclusive. The second node will be between N + 1 and , 2 * N inclusive. Given a list of edges, determine the size of the smallest and largest connected components that have or more nodes. A node can have any number of connections. The highest node valu
View Solution →Kundu and Tree
Kundu is true tree lover. Tree is a connected graph having N vertices and N-1 edges. Today when he got a tree, he colored each edge with one of either red(r) or black(b) color. He is interested in knowing how many triplets(a,b,c) of vertices are there , such that, there is atleast one edge having red color on all the three paths i.e. from vertex a to b, vertex b to c and vertex c to a . Note that
View Solution →Super Maximum Cost Queries
Victoria has a tree, T , consisting of N nodes numbered from 1 to N. Each edge from node Ui to Vi in tree T has an integer weight, Wi. Let's define the cost, C, of a path from some node X to some other node Y as the maximum weight ( W ) for any edge in the unique path from node X to Y node . Victoria wants your help processing Q queries on tree T, where each query contains 2 integers, L and
View Solution →