HDU 1425 sort 题解
选择出数列中前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 题解的更多相关文章
- E题hdu 1425 sort
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 sort Time Limit: 6000/1000 MS (Java/Others) M ...
- hdu 1425 sort 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1425 常规的方法是对输入的数从大到小进行排序(可以用sort或qsort),然后输出前m大的数. 不过 ...
- HDU 1425 sort hash+加速输入
http://acm.hdu.edu.cn/showproblem.php?pid=1425 题目大意: 给你n个整数,请按从大到小的顺序输出其中前m大的数. 其中n和m都是位于[-500000,50 ...
- hdu 1425:sort(排序,经典题。快排模板)
sort Time Limit : 6000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submissi ...
- HDU 1425 sort(堆排序/快排/最大堆/最小堆)
传送门 Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含n个各不 ...
- HDU 1425 sort 【哈希入门】
题意:给出n个数,输出前m大的数 和上一题一样,将输入的数加上一个极大地值作为地址 #include<iostream> #include<cstdio> #include&l ...
- hdu 1425 sort
Problem Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行 ...
- HDU 1425 sort C语言实现快速排序
AC代码:sort Time Limit: 6000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- Hdoj 1425.sort 题解
Problem Description 给你n个整数,请按从大到小的顺序输出其中前m大的数. Input 每组测试数据有两行,第一行有两个数n,m(0<n,m<1000000),第二行包含 ...
随机推荐
- Linux下Qt应用程序的发布(使用LDD命令查看所有依赖的库文件)
最近一直在学习Qt,用Qt写了一个程序,但是不知道怎么发布,网上说的都是在windows下怎么发布Qt应用程序,但是,在windows下Qt应用程序依赖的库文件与linux下的名字不同.于是,我就想到 ...
- win32多线程程序设计笔记(第四章下)
上一笔记讲了同步机制中的临界区域(Critical Sections).互斥器(Mutexes),下面介绍同步机制中的另外两种. 信号量(Semaphores) 举个例子: 现在有人要租车,接待他的代 ...
- uva 10599 - Robots(II) (dp | 记忆化搜索)
本文出自 http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...
- Linux CPU 负载度量公式
一个top命令不就行了么?顶多再加一些管道什么的过滤一下.我一开始也是这么想得.其实还可以理解的更多. 首先一个问题,是统计某个时间点的CPU负载,还是某个时间段的? 为了画折线图报表,一般横坐标都是 ...
- Windows Time服务无法启动 错误5拒绝访问
接着上次写的文章 XP和Win7设置系统自动同步系统时间方法 本文就把故障出现的过程和解决方法一共写下来,希望大家可以看到本文在解决此项服务的思路.大家以后出现类似的问题和问题可以一样使用此类方法解决 ...
- PHPer的等级划分
PHPer的等级划分 前一段时间刚刚完成PHP的培训,然后想知道自己目前的水平(或者说等级),并且应该在哪些方面进行提高,所以在网上查了一下相关介绍.其中有一篇介绍讲的挺清楚的,至少目前的我还是很认同 ...
- 一种用javascript实现的比较兼容的回到顶部demo + 阻止事件冒泡
回到页面顶部demo <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- linux学习: sudo命令(ubuntu)
使用 sudo 命令可以提高命令的执行权限,以root权限执行 如 : sudo vi xxx 但是有些内置命令 如 cd 无法通过 sudo来执行 ,如 sudo cd xxx 这是会报错的. ...
- POJ 2528 QAQ段树+分离
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submitcid=58236#statu ...
- C++11 thread::detach(2)
原文地址:http://www.cplusplus.com/reference/thread/thread/detach/ public member function <thread> ...