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. You know that no individual in that country lives for 100 or more years. Now, you are given a very simple task of sorting all the ages in ascending order.
Input
There are multiple test cases in the input file. Each case starts with an integer n (0<n<=2000000), the total number of people. In the next line, there are n integers indicating the ages. Input is terminated with a case where n = 0. This case should not be processed.
Output
For each case, print a line with n space separated integers. These integers are the ages of that country sorted in ascending order.
Warning: Input Data is pretty big (~ 25 MB) so use faster IO.
Sample Input
5
3 4 2 1 5
5
2 3 2 3 1
0
Output for Sample Input
1 2 3 4 5
1 2 2 3 3
题目大意:给定若干居民的年龄(都是1-100之间的整数),把他们按照从小到大的顺序输出。
分析:年龄排序。由于数据太大,内存限制太紧(甚至都不能把他们全部读进内存),因此无法使用快速排序方法。但整数范围很小,可以用计数排序法。
代码如下:
#include<cstdio>
#include<cstring>
#include<cctype> // 为了使用isdigit宏 inline int readint() {
char c = getchar();
while(!isdigit(c)) c = getchar(); int x = ;
while(isdigit(c)) {
x = x * + c - '';
c = getchar();
}
return x;
} int buf[]; // 声明成全局变量可以减小开销
inline void writeint(int i) {
int p = ;
if(i == ) p++; // 特殊情况:i等于0的时候需要输出0,而不是什么也不输出
else while(i) {
buf[p++] = i % ;
i /= ;
}
for(int j = p-; j >=; j--) putchar('' + buf[j]); // 逆序输出
} int main() {
int n, x, c[];
while(n = readint()) {
memset(c, , sizeof(c));
for(int i = ; i < n; i++) c[readint()]++;
int first = ;
for(int i = ; i <= ; i++)
for(int j = ; j < c[i]; j++) {
if(!first) putchar(' ');
first = ;
writeint(i);
}
putchar('\n');
}
return ;
}
UVA 11462 Age Sort(计数排序法 优化输入输出)的更多相关文章
- Uva-------(11462) Age Sort(计数排序)
B Age Sort Input: Standard Input Output: Standard Output You are given the ages (in years) of all ...
- UVa 11462 Age Sort
解题报告:给若干个居民的年龄排序,年龄的范围在1到100之间,输入的总人数在0到200W.这题要注意的输入的文件约有25MB,而内存限制为2MB,所以如果人数是像200W这样多的话,甚至都不能把它们都 ...
- counting sort 计数排序
//counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...
- 11462 Age Sort(计数排序)
内存不够用,用计数排序可以解决问题. #include<iostream> #include<cstdio> #include<cstdlib> #include& ...
- ACM比赛(11462 Age Sort)
You are given the ages (in years) of all people of a country with at least 1 year of age. You know t ...
- 计数排序(Count Sort )与插入排序(Insert Sort)
计数排序法:计数数组适用于当前数组密集的情况.例如(2,3,5,4,2,3,3,2,5,4) 方法:先找出最大值最小值,之后统计每个数出现的次数,根据次数从小到大往数组里添加 计数排序法是一种不需要比 ...
- COGS 1406. 邻居年龄排序[Age Sort,UVa 11462](水题日常)
★ 输入文件:AgeSort.in 输出文件:AgeSort.out 简单对比时间限制:1 s 内存限制:2 MB [题目描述] Mr.Zero(CH)喜闻乐见地得到了一台内存大大增强 ...
- uva 10474 Where is the Marble? 计数排序
题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...
- CodeForces 558E(计数排序+线段树优化)
题意:一个长度为n的字符串(只包含26个小字母)有q次操作 对于每次操作 给一个区间 和k k为1把该区间的字符不降序排序 k为0把该区间的字符不升序排序 求q次操作后所得字符串 思路: 该题数据规模 ...
随机推荐
- mac osx App store 安装软件 发生错误
app store 安装软件时发生错误 解决办法: 1. 进入如下目录: sh-3.2# cd Library/Application\ Support/App\ Store/ sh-3.2# pwd ...
- Qt5.0.2无法发布问题
先从bin目录里面找到所缺失的dll,如果出现 可以找到libEGL.dll然后复制过来就可以了.
- hdoj 1465 不容易系列之一
转 原文网址 http://blog.csdn.net/liwen_7/article/details/7646451 错排问题 错排问题 就是一种递推式,不过它比较著名且常用,所以要熟记! 方法 ...
- Unity4向上(Unity5)兼容PlayerPrefs的数据存储
好久没有写项目.开发相关的内容了,刚好最近在做项目的更新时,遇到一个比较有意思的坑就随手记录一下. 因为项目的上一上线版本是由Unity5.3发的包,而最新的项目来不及同步更新到5.3版本发包测试,所 ...
- 虚机分配静态IP地址
在为虚机分配静态IP地址池的IP时,如果报错“没有从与 VMSubnet 相关的 IP 池中为虚拟网络适配器 9i [MAC: 001DD8B71C17] 分配 CA (客户地址)” 使用“动态获取” ...
- android学习二(Activity)
前面我简单的介绍了android的一些基础知识,当作热身吧,接下来接触android的四大组件的activity活动. 1.活动Activity是是一种保护用户界面的组件,主要用于和用户进行交互. 活 ...
- hi3531 SDK 编译 uboot, 改动PHY地址, 改动 uboot 參数 .
一,编译uboot SDK文档写得比較清楚了,写一下须要注意的地方吧. 1. 之前用SDK里和别人给的已经编译好的uboot,使用fastboot工具都刷不到板子上.最后自己用SDK里uboot源代码 ...
- android 的异步任务
/** * 异步任务的三个泛型参数: * 1.调用execute方法时传入的参数类型,输入参数 * 2.progressUpdate的方法入参 * 3.异步任务的返回结果类型 doInBackgrou ...
- GROUP BY,WHERE,HAVING之间的差别和使用方法
having子句与where有类似之处但也有差别,都是设定条件的语句.在查询过程中聚合语句(sum,min,max,avg,count)要比having子句优先运行.而where子句在查询过程中运行优 ...
- [译]信仰是怎样毁掉程序猿的How religion destroys programmers
作者原文地址 作者John Sonmez 英文水平不够高,翻译不太准确. 翻译地址:译文 尽管文章是13年的,可是这段时间恰好看到.net开源核心之后,各种java和.net掐架. 语言之争有些牵涉到 ...