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. VellCar(我的钢管车)

    我自己设计了一款钢管车,由于绘图水平有限,所以做的不是特别好看,但整个车架都是我自己设计的,个人觉得不错,尺寸也是按着实际尺寸做的,长2.5米宽为1米,后置发动机,后驱...贴图吧,更形象: 所有零部 ...

  2. Could not load db driver class: com.mysql.jdbc.Driver解决方法

    14/03/26 22:43:24 ERROR sqoop.Sqoop: Got exception running Sqoop: java.lang.RuntimeException: Could ...

  3. ios get airplay name

    tarting from iOS7 AudioToolbox API for currentRoute becomes deprecated: Apple instead made currentRo ...

  4. ios中view的生命周期

  5. 解决IE下iframe默认有白色背景的bug

    又是一个IE莫名其妙的bug:做了一个弹出层,层里面是一张透明图片,IE下的iframe默认会有一个白色的背景,所以当iframe的外围背景并不是白色的时候,这个iframe就会显得非常的“与众不同” ...

  6. php数组相加 两个数组键名相同 后者不能覆盖前者

    array(“a”)+array(“b”)结果还是array(“a”) array(“a”)+array(“b”)的结果是 array(“a”) 因为,它们等效于array(“0″=>”a”)+ ...

  7. Sublime Text 3103 Crack 破解 注册码

    随机复制下面的几四个注册码 粘贴到sublime text 3(Build 3103)注册框 就可以了! ----------------------------------------------- ...

  8. 使用webview如何做超时判断

    在加载网页时给一个timer定时器,规定超时时间,然后再超时时间的方法中提示超时 如果没有超时,则在webview协议中的“加载完成”方法中 取消timer定时器 - (void)openWebVie ...

  9. WPF WebBrowser

      XAML <Window x:Class="WpfApplication5.Window1"     xmlns="http://schemas.microso ...

  10. python的memcache使用如果对key设置了一个int型

    如果对key设置了int型,会出现不可预知的错误,这个问题纠结了我好久,最终还是加了个str(),切忌切忌