codeforces 873D merge sort 构造

本文介绍了一个关于归并排序的问题,即如何构造一个序列使得归并排序的调用次数正好为给定值。文章给出了算法实现,并讨论了解的存在性条件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

D. Merge Sort
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Merge sort is a well-known sorting algorithm. The main function that sorts the elements of array a with indices from [l, r) can be implemented as follows:

If the segment [l, r) is already sorted in non-descending order (that is, for any i such that l ≤ i < r - 1 a[i] ≤ a[i + 1]), then end the function call;
Let ;
Call mergesort(a, l, mid);
Call mergesort(a, mid, r);
Merge segments [l, mid) and [mid, r), making the segment [l, r) sorted in non-descending order. The merge algorithm doesn’t call any other functions.
The array in this problem is 0-indexed, so to sort the whole array, you need to call mergesort(a, 0, n).

The number of calls of function mergesort is very important, so Ivan has decided to calculate it while sorting the array. For example, if a = {1, 2, 3, 4}, then there will be 1 call of mergesort — mergesort(0, 4), which will check that the array is sorted and then end. If a = {2, 1, 3}, then the number of calls is 3: first of all, you call mergesort(0, 3), which then sets mid = 1 and calls mergesort(0, 1) and mergesort(1, 3), which do not perform any recursive calls because segments (0, 1) and (1, 3) are sorted.

Ivan has implemented the program that counts the number of mergesort calls, but now he needs to test it. To do this, he needs to find an array a such that a is a permutation of size n (that is, the number of elements in a is n, and every integer number from [1, n] can be found in this array), and the number of mergesort calls when sorting the array is exactly k.

Help Ivan to find an array he wants!

Input
The first line contains two numbers n and k (1 ≤ n ≤ 100000, 1 ≤ k ≤ 200000) — the size of a desired permutation and the number of mergesort calls required to sort it.

Output
If a permutation of size n such that there will be exactly k calls of mergesort while sorting it doesn’t exist, output  - 1. Otherwise output n integer numbers a[0], a[1], …, a[n - 1] — the elements of a permutation that would meet the required conditions. If there are multiple answers, print any of them.

Examples
input
3 3
output
2 1 3
input
4 1
output
1 2 3 4
input
5 6
output
-1

题意:给出一个归并排序的算法:当前要merge[ l , r)区间,如果这个区间已经有序(升序),那么返回。否则分别merge[ l, mid ) 和merge[ mid , r) 然后再把两段区间给合起来。所以给出一个序列,他调用多少次merge函数是可以确定的。现在给出N和K,要你构造一个N的全排列,使得这个全排列要调用K次merge。无解输出-1。

可以想得到的是 归并排序是一棵树的结构,一个区间无序的话,必定要再访问两个子区间,那么偶数必定是没有解的。然后比较简单粗暴的就是先构造一个1~n的数列,如果这个区间需要变无序,那么就让左边子区间的最后一个,和右边子区间的最左的一个交换,那么子区间会有序,首先把k/2,得到的k就是需要无序的区间,那么递归交换就好
还有一个比较烦的点是左右区间,这里用的是相等的时候左区间少,所以我们使得 l+r+1>>1; 同时取左区间为 (1,mid-1),(mid,r)

#include<bits/stdc++.h>  
using namespace std;  
const int maxn = 1e5+100;  
int a[maxn];  
int n,k;  
void work(int l,int r){  
    if (!k||l>=r-1)return;  
    int mid = l+r>>1;  
    swap(a[mid-1],a[mid]);  
    k--;  
    work(l,mid);work(mid,r);  
}  
int main(){  
    while(~scanf("%d%d",&n,&k))
    { 
        if ((k&1)==0){  
            cout<<-1<<endl;  
            return 0;  
        }  
        k/=2;  
        for (int i=0;i<n;i++){  
            a[i]=i+1;  
        }  
        work(0,n);  
        if (k){  
            cout<<-1<<endl;  
            return 0;  
        }  
        for (int i=0;i<n;i++){  
            printf("%d ",a[i]);  
        }  
    }
    return 0;  
}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值