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),第二行包含 ...
随机推荐
- 实用推荐:12款Linux系统恢复工具
12款Linux系统恢复工具 电脑死机,系统崩溃,总会给电脑使用者带来一定的损失.你是否不小心删除你的纪念图片?安装新系统时候,擦除了分区表?无法读取旧CD里面的数据?别急嘛-我们将会给您推荐一些免费 ...
- Java线程(十):CAS
前言 在Java并发包中有这样一个包,java.util.concurrent.atomic,该包是对Java部分数据类型的原子封装,在原有数据类型的基础上,提供了原子性的操作方法,保证了线程安全.以 ...
- 动态规划---最长上升子序列问题(O(nlogn),O(n^2))
LIS(Longest Increasing Subsequence)最长上升子序列 或者 最长不下降子序列.很基础的题目,有两种算法,复杂度分别为O(n*logn)和O(n^2) . ******* ...
- 枚举最短路径+SPFA
Harry Potter and the Final Battle Submit Status Description The final battle is coming. Now Harry Po ...
- Hbase 配置问题(ERROR: org.apache.hadoop.hbase.PleaseHoldException: org.apache.hadoop.hbase.PleaseHoldEx)
ERROR: org.apache.hadoop.hbase.PleaseHoldException: org.apache.hadoop.hbase.PleaseHoldException: Mas ...
- Solr4.7缓存技术
磁盘IO往往是计算机系统响应速度的一个突出瓶颈,搜索引擎查询很平凡,减少搜索过程中的磁盘IO对提升搜索响应速度无疑有莫大的帮助,在solr中,提供自带的缓存机制.我们只需要在solrconfig.xm ...
- php启用gzip压缩
GZIP(GNU-ZIP)是一种压缩技术.经过GZIP压缩后页面大小可以变为原来的30%甚至更小.这样用户浏览的时候就会感觉很爽很愉快! 要实现GZIP压缩页面需要浏览器和服务器共同支持,实际上就是服 ...
- 设计模式 ( 十五 ) 中介者模式Mediator(对象行为型)
设计模式 ( 十五 ) 中介者模式Mediator(对象行为型) 1.概述 在面向对象的软件设计与开发过程中,根据“单一职责原则”,我们应该尽量将对象细化,使其只负责或呈现单一的职责,即将行为分布到各 ...
- Automake创建项目
autoconf和automake可以方便的构建linux下项目,一个简单的automake项目实例,麻雀虽小五脏俱全,以后无外乎在这基础上扩展相应的宏完善而已. .首先建立项目目录树 )创建目录树 ...
- 基于visual Studio2013解决C语言竞赛题之0520相邻元素
题目