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. 《Android View 的事件分发和滑动冲突》 —预习资料

    1. 阅读书籍<Android开发艺术探索>第三章 2. 提前阅读如下技术文章: http://blog.csdn.net/singwhatiwanna/article/details/3 ...

  2. MYSQL存储过程中的IN、OUT和INOUT

    MYSQL存储过程中的IN.OUT和INOUT,不能简单理解为一个方法的参数和返回值,而是面向整个过程上下文变量的. 一.MySQL 存储过程参数(in) 基本可以理解为传入function的参数,而 ...

  3. VPN两点注意事项

    今天折腾了半天vpn,特记录以下两点注意事项: 1.客户端VPN连接,点右键属性=>网络选项卡=>双击Internet 协议版本 4 IPV4=>高级=>远程网络上使用默认网关 ...

  4. poj 1459 Power Network【建立超级源点,超级汇点】

    Power Network Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 25514   Accepted: 13287 D ...

  5. Centos6.3建立FTP

    2014年2月22日 16:54:20 1. 安装ftp     yum install vsftpd ftp 2. 编辑/etc/vsftpd/vsftpd.conf     chroot_list ...

  6. HTML5初学者福利!11个在线学习网站推荐

    HTML5初学者福利!11个在线学习网站推荐 HTML5的强大及流行趋势,让更多的人想要系统的对它进行学习.而大多数人获取HTML5知识的重要途径都是网络,不过面对五花八门的搜索结果,是不是觉得摸不着 ...

  7. 最火的Android开源项目(一)

    GitHub在中国 的火爆程度无需多言,越来越多的开源项目迁移到GitHub平台上.更何况,基于不要重复造轮子的原则,了解当下比较流行的Android与iOS开源项 目很是必要.利用这些项目,有时能够 ...

  8. asp.net中为什么修改了配置文件后我们不需要重启IIS

    本文转载:http://blog.itpub.net/12639172/viewspace-659819/ 大家知道,asp.net中,如果我们修改了配置文件只要把它保存之后,就会立刻反应到程序中, ...

  9. 微信.NET 微信开发 自己主动内容回复 ASP.NET C#代码

    微信开发中,首先遇到的问题就是处理怎样接收和响应用户消息 , 本文将向大家介绍一下方法和关键的代码. 本文使用的接口库是  :https://github.com/chendong152/Weixin ...

  10. SQL Server将一列的多行内容拼接成一行的问题讨论

    转自http://blog.csdn.net/rolamao/article/details/7745972 昨天遇到一个SQL Server的问题:需要写一个储存过程来处理几个表中的数据,最后问题出 ...