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 (0<n<=2000000), the total number of people. In the next line, there are integers indicating the ages. Input is terminated with a case where = 0. This case should not be processed.

Output

For each case, print a line with 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(计数排序法 优化输入输出)的更多相关文章

  1. Uva-------(11462) Age Sort(计数排序)

    B Age Sort Input: Standard Input Output: Standard Output   You are given the ages (in years) of all ...

  2. UVa 11462 Age Sort

    解题报告:给若干个居民的年龄排序,年龄的范围在1到100之间,输入的总人数在0到200W.这题要注意的输入的文件约有25MB,而内存限制为2MB,所以如果人数是像200W这样多的话,甚至都不能把它们都 ...

  3. counting sort 计数排序

    //counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...

  4. 11462 Age Sort(计数排序)

    内存不够用,用计数排序可以解决问题. #include<iostream> #include<cstdio> #include<cstdlib> #include& ...

  5. 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 ...

  6. 计数排序(Count Sort )与插入排序(Insert Sort)

    计数排序法:计数数组适用于当前数组密集的情况.例如(2,3,5,4,2,3,3,2,5,4) 方法:先找出最大值最小值,之后统计每个数出现的次数,根据次数从小到大往数组里添加 计数排序法是一种不需要比 ...

  7. COGS 1406. 邻居年龄排序[Age Sort,UVa 11462](水题日常)

    ★   输入文件:AgeSort.in   输出文件:AgeSort.out   简单对比时间限制:1 s   内存限制:2 MB [题目描述] Mr.Zero(CH)喜闻乐见地得到了一台内存大大增强 ...

  8. uva 10474 Where is the Marble? 计数排序

    题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...

  9. CodeForces 558E(计数排序+线段树优化)

    题意:一个长度为n的字符串(只包含26个小字母)有q次操作 对于每次操作 给一个区间 和k k为1把该区间的字符不降序排序 k为0把该区间的字符不升序排序 求q次操作后所得字符串 思路: 该题数据规模 ...

随机推荐

  1. HW5.6

    import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...

  2. 打开链接(C# / 默认浏览器)

    System.Diagnostics.Process.Start("http://www.baidu.com/");

  3. Notepad++中的颜色属性大全

    Indent guideline style  缩进参考线的颜色 Brace highlight style 鼠标指针在框架左右时框架的颜色(如css中{}   js中的()) Bad brace c ...

  4. EF查看sql的方法

    using (context = new DataBaseContext()) { #region EF6.0 var listuser =context.dbuser.ToList(); Log.D ...

  5. jqgrid使用简单记录

    我要为id为jqGrid的table使用jqgrid插件. $("#jqGrid").jqGrid({ url: 'data/test.json', mtype: "GE ...

  6. WindDbug应用

    Windbg是windows平台上的一款相当强大的调试工具,可以从msdn网站下载得到,最新版本包含在windows sdk中,默认会被安装在C:\Program Files\Debugging To ...

  7. EditPlus保存文件时不生成其备份文件的方法

    3.将“保存时去掉备份文件”复选框去掉,点击 应用->确定,即可 .

  8. int 0x13中断的參数传递

    int 0x13中断向量所指向的中断服务程序实质上就是磁盘服务程序. 用途:将指定扇区的代码载入到内存的指定位置. 因此,在使用int 0x13中断时要将參数传递给服务程序: 比如:将指定扇区和载入的 ...

  9. MySQL::SQL_MODE

    SQL_MODE可能是比较容易让开发人员和DBA忽略的一个变量,默认为空.SQL_MODE的设置其实是比较冒险的一种设置,因为在这种设置下可以允许一些非法操作,比如可以将NULL插入NOT NULL的 ...

  10. leecode 每日解题思路 64 Minimum Path Sum

    题目描述: 题目链接:64 Minimum Path Sum 问题是要求在一个全为正整数的 m X n 的矩阵中, 取一条从左上为起点, 走到右下为重点的路径, (前进方向只能向左或者向右),求一条所 ...