Description

A well known algorithm called heapsort is a deterministic sorting algorithm taking O(n log n) time and O(1) additional memory. Let us describe ascending sorting of an array of different integer numbers.  The algorithm consists of two phases. In the first phase, called heapification, the array of integers to be sorted is converted to a heap. An array a[1 . . . n] of integers is called a heap if for all 1 <= i <= n the following heap conditions are satisfied:

  • if 2i <= n then a[i] > a[2i];
  • if 2i + 1 <= n then a[i] > a[2i + 1].

We can interpret an array as a binary tree, considering children of element a[i] to be a[2i] and a[2i + 1]. In this case the parent of a[i] is a[i div 2], where i div 2 = [i/2]. In terms of trees the property of being a heap means that for each node its value is greater than the values of its children.  In the second phase the heap is turned into a sorted array. Because of the heap condition the greatest element in the heapified array is a[1]. Let us exchange it with a[n], now the greatest element of the array is at its correct position in the sorted array. This is called extract-max.  Now let us consider the part of the array a[1 . . . n-1]. It may be not a heap because the heap condition may fail for i = 1. If it is so (that is, either a[2] or a[3], or both are greater than a[1]) let us exchange the greatest child of a[1] with it, restoring the heap condition for i = 1. Now it is possible that the heap condition fails for the position that now contains the former value of a[1]. Apply the same procedure to it, exchanging it with its greatest child. Proceeding so we convert the whole array a[1 . . . n-1] to a heap. This procedure is called sifting down. After converting the part a[1 . . . n-1] to a heap by sifting, we apply extract-max again, putting second greatest element of the array to a[n - 1], and so on.  For example, let us see how the heap a = (5, 4, 2, 1, 3) is converted to a sorted array. Let us make the first extract-max. After that the array turns to (3, 4, 2, 1, 5). Heap condition fails for a[1] = 3 because its child a[2] = 4 is greater than it. Let us sift it down, exchanging a[1] and a[2]. Now the array is (4, 3, 2, 1, 5). The heap condition is satisfied for all elements, so sifting is over. Let us make extract-max again. Now the array turns to (1, 3, 2, 4, 5). Again the heap condition fails for a[1]; exchanging it with its greatest child we get the array (3, 1, 2, 4, 5) which is the correct heap. So we make extract-max and get (2, 1, 3, 4, 5). This time the heap condition is satisfied for all elements, so we make extract-max, getting (1, 2, 3, 4, 5). The leading part of the array is a heap, and the last extract-max finally gives (1, 2, 3, 4, 5).  It is known that heapification can be done in O(n) time. Therefore, the most time consuming operation in heapsort algorithm is sifting, which takes O(n log n) time. In this problem you have to find a heapified array containing different numbers from 1 to n, such that when converting it to a sorted array, the total number of exchanges in all sifting operations is maximal possible. In the example above the number of exchanges is 1 + 1 + 0 + 0 + 0 = 2, which is not the maximum. (5, 4, 3, 2, 1) gives the maximal number of 4 exchanges for n = 5.

Input

Input contains n (1 <= n <= 50 000).

Output

Output the array containing n different integer numbers from 1 to n, such that it is a heap, and when converting it to a sorted array, the total number of exchanges in sifting operations is maximal possible. Separate numbers by spaces.

题目大意:构造一个大根堆,使得用这个大根堆从小到大排序的时候交换次数最多。

思路:对于一个n个元素的大根堆,交换1和n,然后进行调整(即交换)。为了交换次数最多,我们希望它能交换到最底层,那个数当然最好是1。然后为了保证深度最大(有可能左边的深度比左边大1),那么我们希望顶元素1调整到n-1的位置。于是开始递推,对于 i-1 个元素最大的情况,最后一位是1,那么我们把1调整到最顶端。然后顶端置 i ,第 i 位置1。那么,我们进行排序到剩下 i 个元素的时候,顶端和位置n交换,剩下 i-1个元素,就必然需要把顶端的1调整到 i-1 的位置。因为我们把1往上调整的时候,比如要把x = heap[j/2]移动到heap[j],那么当1调整到j/2的时候,x肯定是j/2比较大的儿子(因为另一个儿子曾经是x的儿子)。

PS:我用G++交TLE了从C++交AC了你们看着办吧……

代码(391MS):

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL; const int MAXN = ; int heap[MAXN]; int main() {
int n;
while(scanf("%d", &n) != EOF) {
heap[] = ;
for(int i = ; i <= n; ++i) {
for(int j = i - ; j != ; j >>= ) {
heap[j] = heap[j >> ];
}
heap[] = i;
heap[i] = ;
}
for(int i = ; i < n; ++i) printf("%d ", heap[i]);
printf("%d\n", heap[n]);
}
}

POJ 2166 Heapsort(递推)的更多相关文章

  1. poj 2506 Tiling 递推

    题目链接: http://poj.org/problem?id=2506 题目描述: 有2*1和2*2两种瓷片,问铺成2*n的图形有多少种方法? 解题思路: 利用递推思想,2*n可以由2*(n-1)的 ...

  2. POJ 2478 线性递推欧拉函数

    题意: 求sigma phi(n) 思路: 线性递推欧拉函数 (维护前缀和) //By SiriusRen #include <cstdio> using namespace std; # ...

  3. Strange Towers of Hanoi POJ - 1958(递推)

    题意:就是让你求出4个塔的汉诺塔的最小移动步数,(1 <= n <= 12) 那么我们知道3个塔的汉诺塔问题的解为:d[n] = 2*d[n-1] + 1 ,可以解释为把n-1个圆盘移动到 ...

  4. POJ 2506 Tiling (递推 + 大数加法模拟 )

    Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7965   Accepted: 3866 Descriptio ...

  5. poj 2081 简单递推

    #include<stdio.h> #include<string.h> #define N 510000 int dp[N]; int f[10000000]; int ma ...

  6. POJ 1664 放苹果 (递推)

    题目链接:http://poj.org/problem?id=1664 dp[i][j]表示i个盘放j个苹果的方案数,dp[i][j] 可以由 dp[i - 1][j] 和 dp[i][j - i] ...

  7. HOJ 2148&POJ 2680(DP递推,加大数运算)

    Computer Transformation Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4561 Accepted: 17 ...

  8. POJ 2506 Tiling(递推+大整数加法)

    http://poj.org/problem?id=2506 题意: 思路:递推.a[i]=a[i-1]+2*a[i-2]. 计算的时候是大整数加法.错了好久,忘记考虑1了...晕倒. #includ ...

  9. POJ 1661 Help Jimmy(递推DP)

    思路: 1. 每个板子有左右两端, dp[i][0], dp[i][1] 分别记录左右端到地面的时间 2. 从下到上递推计算, 上一层的板子必然会落到下面的某一层板子上, 或者地面上 总结: 1. 计 ...

随机推荐

  1. git submodule update --init --recursive

    最近在跑好几个模型,视频检测,物体检测,搭建mxnet时,有点问题,记录一下. 视频检测,mxnet需要用指定版本,git 切换到指定版本后,update了,但是依然提示说有些库找不到.想了想,应该是 ...

  2. 在Azure上部署Sqlserver网络访问不了的问题

    最近在部署Azure虚拟机的时候,一直访问不了网络数据库,一搜资料才知道,Azure默认是不打开入网规则的,需要手动设置. 在 Windows 防火墙中为数据库引擎的默认实例打开 TCP 端口 在“开 ...

  3. 菜鸟笔记 -- Chapter 6.2.4 成员方法

    6.2.4  成员方法 在Java中使用成员方法对应于类对象的行为,在有些地方也会将方法称之为函数,成员方法是定义在类中具有特定功能的一段独立小程序.方法格式如下: 修饰符 返回值类型 成员方法名 ( ...

  4. UIlable上下居中

    - (void)setView{ self.chooseImg = [[UIImageView alloc] initWithFrame:CGRectMake(TO_LEFT_D, (ROW_HEIG ...

  5. iOS实现电话状态监听 CoreTelephony

    在程序中如果需要监听电话状态,可以引入CoreTelephony框架,这个框架包含了电话相关的API,可以实现监测来电,查看运营商信息等功能.下面就是具体的实现监测来电的代码.一定要把center写成 ...

  6. iOS之查看代码运行的时间

    有时候我们想要准确的知道某段代码.某个循环执行的时间,然后分析效率等问题,这个时候就需要执行时间是多少.正好看到网上已经有人做了这个工作,我就直接摘下来了.正好也用了宏的方式计算时间,我们只要在需要计 ...

  7. 【TOJ 3692】紧急援救

    #include<iostream> #include<algorithm> #include<queue> using namespace std; #defin ...

  8. boost::asio::ip::tcp中几个重要类型

    typedef basic_stream_socket socket; 流式套接字,提供同/异步发送接收数据,连接,绑定,设置套接字选项等功能 对于socket中的connect()方法,它只针对某一 ...

  9. 快速玩转linux(4)

    websever安装配置 Nginx & Apache 并发量. Apache基本操作 解释 命令 安装 yum install httpd 启动 service httpd start 停止 ...

  10. Redis安装与简单配置

    一.Redis介绍 1.redis是什么? remote dIctionary server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redis提 ...