选择出数列中前k个最大的数。

这里由于数据特殊。所以能够使用hash表的方法:

#include <cstdio>
#include <algorithm>
#include <stdlib.h>
#include <limits>
using namespace std; const int SIZE = 1000005;
const int SMALL = -500000;
bool arr[SIZE]; int main()
{
int n, m, a, maxN = INT_MIN;
while (~scanf("%d %d", &n, &m))
{
for (int i = 0; i < n; i++)
{
scanf("%d", &a);
arr[a - SMALL] = true;
if (a - SMALL > maxN) maxN = a - SMALL;
}
for (int i = maxN; i >= 0 && m; i--)
{
if (arr[i])
{
if (m > 1) printf("%d ", i + SMALL);
else printf("%d\n", i + SMALL);
m--;
}
}
}
return 0;
}

也能够使用选择前k个数,然后排序,只是难度高非常多:

#include <cstdio>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std; int partition(int *arr, int low, int up)
{
for (int i = low; i < up; i++)
if (arr[i] >= arr[up]) swap(arr[low++], arr[i]); swap(arr[low], arr[up]);
return low;
} //K作为绝对下标位置处理
void randSelectK(int *arr, int low, int up, int K)
{
int r = low + rand() % (up - low + 1);
swap(arr[r], arr[up]); int idx = partition(arr, low, up);
if (idx > K) randSelectK(arr, low, idx-1, K);
else if (idx < K) randSelectK(arr, idx+1, up, K);
} void randQuickSort(int *arr, int low, int up)
{
if (low >= up) return ;//不能是low == up int r = low + rand() % (up - low + 1);
swap(arr[r], arr[up]); int mid = partition(arr, low, up);
randQuickSort(arr, low, mid-1);
randQuickSort(arr, mid+1, up);
} const int SIZE = 1000000;
int arr[SIZE];
int main()
{
int n, m;
srand(unsigned(time(NULL)));
while(~scanf("%d %d", &n, &m))
{
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]); randSelectK(arr, 0, n-1, m-1);
randQuickSort(arr, 0, m-1);
for (int i = 0; i < m-1; i++)
{
printf("%d ", arr[i]);
}
printf("%d\n", arr[m-1]);
}
return 0;
}

HDU 1425 sort 题解的更多相关文章

  1. E题hdu 1425 sort

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

  2. hdu 1425 sort 解题报告

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

  3. HDU 1425 sort hash+加速输入

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

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

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

  5. HDU 1425 sort(堆排序/快排/最大堆/最小堆)

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

  6. HDU 1425 sort 【哈希入门】

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

  7. hdu 1425 sort

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

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

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

  9. Hdoj 1425.sort 题解

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

随机推荐

  1. python urllib和urllib2 区别

    python有一个基础的库叫httplib.httplib实现了HTTP和HTTPS的客户端协议,一般不直接使用,在python更高层的封装模块中(urllib,urllib2)使用了它的http实现 ...

  2. 基于visual Studio2013解决C语言竞赛题之0404循环求和

      题目 解决代码及点评 这道题考验for循环和一个简单的算法 因为每次累加的值有规律,后面一次累加是前面一次累加的两倍 所以可以用简单的循环,计算累加项和累加结果 /************ ...

  3. Maven--几个需要补充的问题(三)

    <Maven--搭建开发环境(一)> <Maven--构建企业级仓库(二)> <Maven—几个需要补充的问题(三)> 前两篇由于篇幅太长,为了给读者理解方便,这篇 ...

  4. <jsp:include page="">和<%@include page=""%> 标签学习

    <jsp:include page=""><jsp:param value=""name=""/><DEL&g ...

  5. Objective-C中的类目(Category),延展(Extension)

    类目和延展的作用都是为了扩展一个类. Objective-C中的类目(Category) 一.类目的定义和作用 类目也叫分类,英文Category,在没有原类.m文件的基础上,给该类添加方法. 比如, ...

  6. Ubuntu 13.04 安装使用clang

    其实很简单,就是用命令即可: apt-get install clang-3.2 clang-3.2-doc 主要说明一点,/usr/bin/c++链接原来指向g++,现在被改变了. $ ls /us ...

  7. 【Hibernate】Illegal attempt to associate a collection with two open sessions

    今天在用Hibernate对对象进行修改操作的时候报了这个错. 之前一直没什么错误,但是今天修改了一下表结构,增加了一个OneToMany的映射. 所以在我获取对象,重新set一个变量之后就报了这个错 ...

  8. hdu2629Identity Card

    Problem Description Do you own an ID card?You must have a identity card number in your family's Hous ...

  9. django学习之Model(四)MakingQuery

    上一篇写到MakingQuey中的filter,本篇接着来. 10)-扩展多值的关系 如果对一个ManyToManyField或ForeignKey的表进行filter过滤查询的话,有2中方法可以用. ...

  10. Krita 3.0 发布,KOffice 的图像处理器(刺激一下自己的神经)

    Krita 3.0 发布了,经历了一年多的开发,动画功能被集成到Krita核心,改善了绘画功能,可及时预览绘画结果,该版本也是最新移植到QT的版本. 查看完整发布说明,可以点击这里. 下载地址: Wi ...