选择出数列中前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. 异步和同步http请求超时机制

    异步超时设置: 例子: Example: do a simple HTTP GET request for http://www.nethype.de/ and print the response ...

  2. python手记(41)

    python opencv图片融合 #!/usr/bin/env python #-*- coding: utf-8 -*- #code:myhaspl@qq.com import cv2 impor ...

  3. WinForm界面中快捷键设置

    这是对整个界面的快捷键的设置,比如查询,保存. 1 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if ...

  4. Codeforces Round #316 (Div. 2A) 570A Elections

    题目:Click here #include <bits/stdc++.h> using namespace std; typedef long long ll; const int IN ...

  5. docker 学习笔记21:docker连接网络的设置

    1.如果docker主机不需要通过代理连接外网 则docker的相关命令(如docker search)或docker容器与网络相关的操作都可以正常进行,不需要特殊设置. 2.当docker主机 是通 ...

  6. Android常用动画alpha和rotate同时使用

    Android的动画可以是一种动画,也可以多种动画作用于一张图片上,如RotaeAnimation和AlphaAnimation同时放到一个配置文件中 alpha1.xml <?xml vers ...

  7. JAVA操作Hbase基础例子

    package com.cma.hbase.test; import java.io.BufferedInputStream; import java.io.BufferedReader; impor ...

  8. Linux 下IOport编程訪问

    曾经写的一篇笔记.偶尔翻出来了,放在这里做个纪念 Linux 下IOport编程訪问 这里记录的方法是在用户态訪问IOport,不涉及驱动程序的编写. 首先要包括头文件 /usr/include/as ...

  9. NanShan即时通讯论——HTML5的优势与劣势

    原文:NanShan即时通讯论--HTML5的优势与劣势 NanShan即时通讯 学习html时才是XHTML 1.0,接着是 HTML4.01,再到HTML5,如今HTML5 開始吸引越来越多的人的 ...

  10. SqlServer字段说明查询(表基本信息查询)

    --快速查看表结构(比较全面的) THEN obj.name ELSE '' END AS 表名, col.colorder AS 序号 , col.name AS 列名 , ISNULL(ep.[v ...