sort(hdu oj 1425)计数排序和快速排序
Description
给你n个整数,请按从大到小的顺序输出其中前m大的数。
Input
每组测试数据有两行,第一行有两个数n,m(0 < n,m < 1000000),第二行包含n个各不相同,且都处于区间[-500000,500000]的整数。
Output
对每组测试数据按从大到小的顺序输出前m大的数。
Sample Input
5 3
3 -35 92 213 -644
Sample Output
213 92 3
emm
一开始呢,用快速排序找前m大的数
一提交,超时……淦
后来度娘一搜
原来有计数排序,用空间换时间
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int num[1000001];
int main()
{
int n, m, i, j, temp;
while (~scanf("%d%d", &n, &m))
{
memset(num, 0, sizeof(num));
for (i = 0; i < n; i++)
{
scanf("%d", &temp);
num[temp + 500000]++;
}
j = sizeof(num) / sizeof(int) - 1;
for (i = 0; i < m; i++)
{
if (i > 0)
printf(" ");
while (num[j] == 0)
j--;
printf("%d", j - 500000);
num[j]--;
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <memory.h>
unsigned char num[1000001];
int main()
{
int n, m, i, j = 1000000, t;
while (~scanf("%d%d", &n, &m))
{
memset(num, 0, sizeof(num));
while (n--)
{
scanf("%d", &t);
num[t + 500000]++;
}
while (m--)
{
while (num[j] == 0)
j--;
printf("%d%c", j - 500000, m == 0 ? '\n' : ' ');
num[j]--;
}
}
return 0;
}
这是快速排序的代码(hdu oj G++过了,sdtbu oj C++超时的):
#include <cstdio>
#include <cstdlib>
int num[1000000];
void quictSort(int, int, int);
int partition(int, int);
int main()
{
int n, m, i;
while (~scanf("%d%d", &n, &m))
{
for (i = 0; i < n; i++)
scanf("%d", &num[i]);
quictSort(0, n - 1, m - 1);
/*
for (i = 0; i < n; i++)
printf("%d ", num[i]);
printf("\n", num[i]);
printf("*******\n");
*/
for (i = 0; i < m - 1; i++)
printf("%d ", num[i]);
printf("%d\n", num[i]);
}
return 0;
}
// 利用快速排序找前m大的数
void quictSort(int left, int right, int mTop)
{
if (left < right)
{
int p = partition(left, right); // 分两段
int len = p - left;
quictSort(left, p - 1, mTop); // 左半段排序
if (len < mTop)
quictSort(p + 1, right, mTop - len); // 右半段排序
}
}
// 从大到小排序
int partition(int left, int right)
{
int key = num[left]; // 第一个元素为基准元素
while (left < right)
{
while (left < right && num[right] <= key) // 从右往左找到比基准元素大的
right--;
if (left < right)
num[left] = num[right]; // 把大的交换到左边
while (left < right && num[left] >= key) // 从左往右找到比基准元素小的
left++;
if (left < right)
num[right] = num[left]; // 把小的交换到右边
}
num[left] = key; // 把基准元素赋值回去
return left;
}
sort(hdu oj 1425)计数排序和快速排序的更多相关文章
- HDU-1425-sort(计数排序以及快速排序和堆排序的变种)
计数排序 Accepted 1425 483MS 5276K 997 B G++ #include "bits/stdc++.h" using namespace std; typ ...
- 八大排序方法汇总(选择排序,插入排序-简单插入排序、shell排序,交换排序-冒泡排序、快速排序、堆排序,归并排序,计数排序)
2013-08-22 14:55:33 八大排序方法汇总(选择排序-简单选择排序.堆排序,插入排序-简单插入排序.shell排序,交换排序-冒泡排序.快速排序,归并排序,计数排序). 插入排序还可以和 ...
- UVA 11462 Age Sort(计数排序法 优化输入输出)
Age Sort You are given the ages (in years) of all people of a country with at least 1 year of age. Y ...
- Uva-------(11462) Age Sort(计数排序)
B Age Sort Input: Standard Input Output: Standard Output You are given the ages (in years) of all ...
- 计数排序(Count Sort )与插入排序(Insert Sort)
计数排序法:计数数组适用于当前数组密集的情况.例如(2,3,5,4,2,3,3,2,5,4) 方法:先找出最大值最小值,之后统计每个数出现的次数,根据次数从小到大往数组里添加 计数排序法是一种不需要比 ...
- counting sort 计数排序
//counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...
- 《算法导论》——计数排序Counting Sort
今天贴出的算法是计数排序Counting Sort.在经过一番挣扎之前,我很纠结,今天这个算法在一些scenarios,并不是最优的算法.最坏情况和最好情况下,时间复杂度差距很大. 代码Countin ...
- 计数排序与桶排序(bucket sort)
Bucket Sort is a sorting method that subdivides the given data into various buckets depending on cer ...
- 排序算法六:计数排序(Counting sort)
前面介绍的几种排序算法,都是基于不同位置的元素比较,算法平均时间复杂度理论最好值是θ(nlgn). 今天介绍一种新的排序算法,计数排序(Counting sort),计数排序是一个非基于比较的线性时间 ...
随机推荐
- spring boot:redis+lua实现生产环境中可用的秒杀功能(spring boot 2.2.0)
一,秒杀需要具备的功能: 秒杀通常是电商中用到的吸引流量的促销活动方式 搭建秒杀系统,需要具备以下几点: 1,限制每个用户购买的商品数量,(秒杀价格为吸引流量一般会订的很低,不能让一个用户全部抢购到手 ...
- ImageMagick实现图片加水印(ImageMagick6.9.10)
一,ImageMagick的安装 请参见: https://www.cnblogs.com/architectforest/p/12807514.html 说明:刘宏缔的架构森林是一个专注架构的博客, ...
- ImageMagick实现图片的旋转/翻转/裁剪(ImageMagick6.9.10)
一,imagemagick的安装 请参见: https://www.cnblogs.com/architectforest/p/12807514.html 说明:刘宏缔的架构森林是一个专注架构的博客, ...
- http请求需要了解的一些信息
http请求需要了解的一些信息 http请求头:https://jingyan.baidu.com/article/375c8e19770f0e25f2a22900.htmlhttp状态码 :http ...
- 一个例子"入坑"布谷鸟算法(附完整py代码)
布谷鸟是比较新的启发式最优化算法,但其与传统的遗传算法,退火算法等相比,被证明收敛速度更快,计算效率更高! 文章目录 本文诞生的缘由 布谷鸟算法思想简介 更新位置的方式 莱维飞行 局部随机行走 抛出个 ...
- Go 包管理历史以及 Go mod 使用
之前也写过 Go 管理依赖工具 godep 的使用,当时看 godep 使用起来还是挺方便,其原因主要在于有总比没有强.关于依赖管理工具其实还是想从头聊聊这个需求以及大家做这个功能的各种出发点. GO ...
- Solr入门-Solr服务安装(windows系统)
安装Solr 首先保证已经正确安装了Java 下载Solr,当前最新版6.1.0 Solr各个版本下载地址 Solr从6.0之后需要Java1.8所以如果使用Solr6.0及其以上版本,请确保Java ...
- Eureka整合sidecar异构调用
本次使用nodejs脚本生成的异构程序测试: node-server.js var http = require('http'); var url = require('url'); var path ...
- Hadoop基础------>MR框架-->WordCount
认识Mapreduce Mapreduce编程思想 Mapreduce执行流程 java版本WordCount实例 1. 简介: Mapreduce源于Google一遍论文,是谷歌Mapreduce的 ...
- Linux下的django项目02
3.创建user模型 3.1 创建用户模型user 第一步 django-admin startproject syl 第二 在syl下创建apps文件包并标记根源 cd 到apps下并进行以下步骤 ...