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),第二行包含 ...
随机推荐
- Putty远程登录VMware虚拟机Linux(Ubuntu12.04)
为了不至于来回在Win7和Ubuntu12.04之间来回切换,在Win7下使用VMware9.0安装了Ubuntu12.04. 首先下载Vmware9.0虚拟机软件,下载地址为:VMware-work ...
- [置顶] ios 360度旋转效果demo
demo功能:用UIimageView实现360度旋转效果. demo说明:iPhone6.1 测试成功.主要代码在:FVImageSequence.m中.在touchesMoved事件中,通过替换U ...
- [置顶] android AIDL 进程间通信
1.定义aidl文件 a.ITestService.aidl package com.open.aidl.service; import com.open.aidl.service.ITestServ ...
- nodejs笔记2——请求路由
对于不同的URL请求,服务器应该有不同的反应.我们要为路由提供请求的URL和其他需要的GET及POST参数,随后路由需要根据这些数据来执行相应的代码.我们需要的所有数据都会包含在request对象中, ...
- 【Oracle】SQL*Loader-522: lfiopn failed for file
Linux下使用sqlldr进行批量操作,此操作会自动生成和删除临时文件. 因此,当前操作的用户必须具备对存放文件的文件夹有增删改的权限. 使用root登录,修改改文件夹权限为777即可. chmod ...
- iOS UIWebView 之 UIProgressView
之前做等待跳转都是用UIActivityIndicatorView ,后来做webView 加载页面的时候,发现了一个特别好用又超级炫酷的加载提示NJKWebViewProgress,作者巧妙的通过计 ...
- CodeForces 508C Anya and Ghosts 贪心
做不出题目,只能怪自己不认真 题目: Click here 题意: 给你3个数m,t,r分别表示鬼的数量,每只蜡烛持续燃烧的时间,每个鬼来时要至少亮着的蜡烛数量,接下来m个数分别表示每个鬼来的时间点( ...
- javascript (string 部分)
<html> <body> <script type="text/javascript"> var str="ab:cd:ef:gh& ...
- jquery ajax json 数据的遍历
需求:进行ajax请求后,后台传递回来以下json数据 { "data":[ {","name":"选择A","valu ...
- log4j的使用及参考
log4j.properties 使用 一.参数意义说明 输出级别的种类 ERROR.WARN.INFO.DEBUG ERROR 为严重错误 主要是程序的错误 WARN 为一般警告,比如session ...