传送门

Description

给你n个整数,请按从大到小的顺序输出其中前m大的数。

Input

每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。

Output

对每组测试数据按从大到小的顺序输出前m大的数。

Sample Input

5 3 3 -35 92 213 -644

Sample Output

213 92 3

1、使用内建sort函数

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;
int ans[1000001];
int main()
{
    int n,m;
    while (scanf("%d%d",&n,&m) != EOF)
    {
        for (int i = 0;i < n;i++)
        {
            scanf("%d",&ans[i]);
        }
        sort(ans,ans + n);
        printf("%d",ans[n - 1]);
        for(int i = n - 2;m > 1;m--,i--)
        {
            printf(" %d",ans[i]);
        }
        printf("\n");
    }
    return 0;
}

2、使用内建优先队列

#include<iostream>
#include<queue>
#include<cstdio>
using namespace std;

int main()
{
    int N,M;
    while (~scanf("%d%d",&N,&M))
    {
        priority_queue<int>que;
        bool first = true;
        int tmp;
        while (N--)
        {
            scanf("%d",&tmp);
            que.push(tmp);
        }
        while (M--)
        {
            first?printf("%d",que.top()):printf(" %d",que.top());
            que.pop();
            first = false;
        }
        printf("\n");
    }
    return 0;
}

3、使用内部堆函数

#include <cstdio>
#include <algorithm>
using namespace std;

static int a[1000000];

int main()
{
    int i,n,m;
    while(EOF != scanf("%d %d",&n,&m))
    {
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        make_heap(a,a+n);
        printf("%d",a[0]);
        for(i=1;i<m;i++)
        {
            pop_heap(a,a+n-i+1);
            printf(" %d",a[0]);
        }
        printf("\n");
    }
    return 0;
}

4、手写最大堆

#include<stdio.h>
#include<string.h>
const int maxn = 1000005;
int heap[maxn],sz = 0;

void push(int x)
{
    int i = sz++;
    while (i > 0)
    {
        int p = (i - 1)/2;
        if (heap[p] >= x)    break;
        heap[i] = heap[p];
        i = p;
    }
    heap[i] = x;
}

int pop()
{
    int ret = heap[0];
    int x = heap[--sz];
    int i = 0;
    while (i*2+1<sz)
    {
        int a = i*2+1,b = i*2+2;
        if (b < sz && heap[b]>heap[a])    a = b;
        if (heap[a] <= x)    break;
        heap[i] = heap[a];
        i = a;
    }
    heap[i] = x;
    return ret;
}

int main()
{
    int n,m,tmp;
    while (~scanf("%d%d",&n,&m))
    {
        sz = 0;
        memset(heap,0,sizeof(heap));
        for (int i = 0;i < n;i++)    scanf("%d",&tmp),push(tmp);
        for (int i = 1;i < m;i++)    printf("%d ",pop());
        printf("%d\n",pop());
    }
    return 0;
} 

5、手写堆排序(从大到小)

#include<bits/stdc++.h>
using namespace std;
#define LeftChild(i) (2*(i)+1)
const int maxn = 1000005;
int a[maxn]; 

void PercDown(int a[],int i,int N)
{
    int Child;
    int tmp;
    for (tmp = a[i];LeftChild(i) <N;i = Child)
    {
        Child = LeftChild(i);
        if (Child != N - 1 && a[Child+1] < a[Child])
            Child++;
        if (tmp > a[Child])
            a[i] = a[Child];
        else
            break;
    }
    a[i] = tmp;
}

void Heapsort(int a[],int N)
{
    int i;
    for (i = N/2;i >= 0;i--)
        PercDown(a,i,N);
    for (i = N-1;i > 0;i--)
    {
        swap(a[0],a[i]);
        PercDown(a,0,i);
    }
}

int main()
{
    int n,m;
    while (~scanf("%d%d",&n,&m))
    {
        for (int i = 0;i < n;i++)    scanf("%d",&a[i]);
        Heapsort(a,n);
        printf("%d",a[0]);
        for (int i = 1;i < m;i++)    printf(" %d",a[i]);
        printf("\n");
    }
    return 0;
}

6、手写堆排序(从小到大)

#include<cstdio>
#include<algorithm>
using namespace std;
#define LeftChild(i) (2*(i)+1)
const int maxn = 1000005;
int a[maxn]; 

void PercDown(int a[],int i,int N)
{
    int Child;
    int tmp;
    for (tmp = a[i];LeftChild(i) <N;i = Child)
    {
        Child = LeftChild(i);
        if (Child != N - 1 && a[Child+1] > a[Child])
            Child++;
        if (tmp < a[Child])
            a[i] = a[Child];
        else
            break;
    }
    a[i] = tmp;
}

void Heapsort(int a[],int N)
{
    int i;
    for (i = N/2;i >= 0;i--)
        PercDown(a,i,N);
    for (i = N-1;i > 0;i--)
    {
        swap(a[0],a[i]);
        PercDown(a,0,i);
    }
}

int main()
{
    int n,m;
    while (~scanf("%d%d",&n,&m))
    {
        for (int i = 0;i < n;i++)    scanf("%d",&a[i]);
        Heapsort(a,n);
        printf("%d",a[n-1]);
        for (int i = n-2;i > n-1 - m;i--)    printf(" %d",a[i]);
        printf("\n");
    }
    return 0;
}

7、手写快速排序(从大到小)

#include<bits/stdc++.h>
using namespace std;
#define Cutoff (3)
const int maxn = 1000005;
int a[maxn];

void InsertionSort(int a[],int N)
{
    int i,j,tmp;
    for (i = N - 2;i >= 0;i--)
    {
        tmp = a[i];
        for (j = i;j < N - 1 && a[j + 1] > tmp;j++)
            a[j] = a[j + 1];
        a[j] = tmp;
    }
}

int median3(int a[],int left,int right)
{
    int center = (left + right) / 2;
    if (a[left] < a[center])
        swap(a[left],a[center]);
    if (a[left] < a[right])
        swap(a[left],a[right]);
    if (a[center] < a[right])
        swap(a[center],a[right]);
    swap(a[center],a[left + 1]);
    return a[left + 1];
}

void qsort(int a[],int left,int right)
{
    int i,j,pivot;
    if (left + Cutoff <= right)
    {
        pivot = median3(a,left,right);
        i = left + 1;j = right;
        for (;;)
        {
            while (a[++i] > pivot){}
            while (a[--j] < pivot){}
            if (i < j)    swap(a[i],a[j]);
            else    break;
        }
        swap(a[j],a[left + 1]);
        qsort(a,left,j - 1);
        qsort(a,j + 1,right);
    }
    else    InsertionSort(a + left,right - left + 1);
}

int main()
{
    int n,m;
    while (~scanf("%d%d",&n,&m))
    {
        for (int i = 0;i < n;i++)    scanf("%d",&a[i]);
        qsort(a,0,n - 1);
        printf("%d",a[0]);
        for (int i = 1;i < m;i++)    printf(" %d",a[i]);
        printf("\n");
    }
    return 0;
}

8、手写快速排序(从小到大)

#include<bits/stdc++.h>
using namespace std;
const int Cutoff = 10;
const int maxn = 1000005;
int a[maxn];
void InsertionSort(int a[],int N)
{
    int i,j,tmp;
    for (i = 1;i < N;i++)
    {
        tmp = a[i];
        for (j = i;j > 0 && a[j - 1] > tmp;j--)
            a[j] = a[j - 1];
        a[j] = tmp;
    }
}
int median3(int a[],int left,int right)
{
    int center = (left + right) / 2;
    if (a[left] > a[center])
        swap(a[left],a[center]);
    if (a[left] > a[right])
        swap(a[left],a[right]);
    if (a[center] > a[right])
        swap(a[center],a[right]);
    swap(a[center],a[right-1]);
    return a[right-1];
}
void qsort(int a[],int left,int right)
{
    int i,j,pivot;
    if (left + Cutoff <= right)
    {
        pivot = median3(a,left,right);
        i = left;j = right-1;
        for (;;)
        {
            while (a[++i] < pivot){}
            while (a[--j] > pivot){}
            if (i < j)    swap(a[i],a[j]);
            else    break;
        }
        swap(a[i],a[right-1]);
        qsort(a,left,i - 1);
        qsort(a,i + 1,right);
    }
    else    InsertionSort(a + left,right - left + 1);
}
int main()
{
    int n,m;
    while (~scanf("%d%d",&n,&m))
    {
        for (int i = 0;i < n;i++)    scanf("%d",&a[i]);
        qsort(a,0,n - 1);
        printf("%d",a[n - 1]);
        for (int i = n - 2;i > n - m - 1;i--)    printf(" %d",a[i]);
        printf("\n");
    }
    return 0;
}

  

HDU 1425 sort(堆排序/快排/最大堆/最小堆)的更多相关文章

  1. 最大堆 最小堆 解决TOPK问题

    堆:实质是一颗完全二叉树,最大堆的特点:父节点值均大于子节点:最小堆的父节点值均小于子节点: 一般使用连续内存存储堆内的值,因而可以根据当前节点的索引值推断子节点的索引值: 节点i的父节点为(i-1) ...

  2. hdu 1425:sort(排序,经典题。快排模板)

    sort Time Limit : 6000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissi ...

  3. E题hdu 1425 sort

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 sort Time Limit: 6000/1000 MS (Java/Others)    M ...

  4. Black Box--[优先队列 、最大堆最小堆的应用]

    Description Our Black Box represents a primitive database. It can save an integer array and has a sp ...

  5. hdu 1425 sort 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 常规的方法是对输入的数从大到小进行排序(可以用sort或qsort),然后输出前m大的数. 不过 ...

  6. HDU 1425 sort hash+加速输入

    http://acm.hdu.edu.cn/showproblem.php?pid=1425 题目大意: 给你n个整数,请按从大到小的顺序输出其中前m大的数. 其中n和m都是位于[-500000,50 ...

  7. HDU 1425 sort 【哈希入门】

    题意:给出n个数,输出前m大的数 和上一题一样,将输入的数加上一个极大地值作为地址 #include<iostream> #include<cstdio> #include&l ...

  8. hdu 1425 sort

    Problem Description 给你n个整数,请按从大到小的顺序输出其中前m大的数.   Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行 ...

  9. HDU 1425 sort C语言实现快速排序

    AC代码:sort Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...

随机推荐

  1. Java 基础【11】@注解

    1.注解简介 JDK 1.5 中引入的 java.lang.annotation 包提供注解编程支持,可以让类在编译.类加载.运行时被读取,并执行相应的处理. 在 Java EE应用的时候,总是免不了 ...

  2. Java 基础【08】.class getClass () forName() 详解

    类名.class是Class对象的句柄,每个被加载的类,在jvm中都会有一个Class对象与之相对应. 如果要创建新的对象,直接使用Class对象的局部class.forName就可以了,不需要用ne ...

  3. 2016年优秀的java网站分享

    java中文网站 伯乐在线java版:http://www.importnew.com/ 码农网:http://www.codeceo.com/ infoq:http://www.infoq.com/ ...

  4. 关于BOM

    BOM:浏览器对象模型 (Browser Object Model)主要定义的是JS操作浏览器的方法和属性. 大部分方法都在window下. 常用方法:(JS里面规定如果方法前面是window,win ...

  5. 一次莽撞的行为:在phpmyadmin中修改MySQL root密码后无法操作数据库

    一.手贱行为(✿◡‿◡) 在一次开发中通过xampp方式安装了PHP环境,需要操作数据库时通过phpmyadmin访问MySQL,在一次数据库操作时想起没有设置密码,于是直接在mysql数据库中的us ...

  6. Javascript DOM操作实例

          最近在学DOM,但是还是没有办法很好的记住API,想找些例子来练习,网上的例子将一个个DOM对象方法挨个举例,并没有集合在一起用,效果不尽人意.所以自己写一份实例,顺便巩固下学到的知识. ...

  7. iostat命令详解

    iostat iostat用于输出CPU和磁盘I/O相关的统计信息. 命令格式: iostat [ -c | -d ] [ -k | -m ] [ -t ] [ -V ] [ -x ] [ devic ...

  8. [BZOJ 1260][CQOI2007]染色(DP)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1260 分析: f[i][j]表示i~j刷成s[i]~s[j]这个样子需要的最小次数 则 ...

  9. Hibernate原生SQL映射MySQL的CHAR(n)类型到String时出错

    今天在用Hibernate通过原生SQL和ResultTransformer映射时,出现数据类型不匹配的错误.但是通过Entity映射,没有问题.在网上找了好多答案,终于解决了. 核心代码: Stri ...

  10. js浏览器窗口

    一.clientwidth和clientheight 注:返回了元素大小,但没有单位,默认单位是 px,如果你强行设置了单位,比如 100em之类,它还是会返回 px的大小. (CSS获取的话,是照着 ...