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


B


Age Sort

Input: Standard Input

Output: Standard Output

 

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                             Output for Sample Input


5

3 4 2 1 5

5

2 3 2 3 1

0


1 2 3 4 5

1 2 2 3 3

Note: The memory limit of this problem is 2 Megabyte Only.


Problem Setter: Mohammad Mahmudur Rahman

Special Thanks: Shahriar Manzoor

数据大,内存小,而数据值的范围有限,适合运用计数排序求解...

代码:

 1 #include<cstdio>
 2 #include<cstring>
 3 /*计数排序*/
 4 int main()
 5 {
 6     int n,hash[101],val;
 7     while(~scanf("%d",&n)&&n)
 8     {
 9        memset(hash,0,sizeof(hash));
10        for(int i=0;i<n;i++)
11        {
12          scanf("%d",&val);
13          hash[val]++;
14        }
15        int flag=true;
16        for(int i=0; i<=100; i++)
17        {
18          for(int j=1;j<=hash[i];j++)
19          {
20              if(flag)
21              {
22                  printf("%d",i);
23                  flag=false;
24              }
25              else printf(" %d",i);
26          }
27        }
28        puts("");
29     }
30     return 0;
31 }

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

时间: 2024-10-10 05:53:11

Uva-------(11462) Age Sort(计数排序)的相关文章

[2016-03-19][UVA][11462][Age Sort]

时间:2016-03-19 20:22:57 星期六 题目编号:[2016-03-19][UVA][11462][Age Sort] 题目大意:给定n个数字,输出排序后的数据, 分析:n<=2*1E6,多组数据,所以不能直接读取然后排序,这里的数据内容范围是1~100,则可以通过计数的方式来统计,或者自己编写读取和输出的函数来优化 方法2 #include <cstdio> #include <cstring> using namespace std; typedef lon

UVA - 11462 - Age Sort (高效算法!!)

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 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 ascendi

UVA之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 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.

counting sort 计数排序

//counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorithm> #include<cassert> using namespace std; const int k=5; const int n=7; int a[n]={5, 5, 1, 2 , 5, 4, 1}; int b[n]; int c[k+1]; int main() { int m

11462 Age Sort(计数排序)

内存不够用,用计数排序可以解决问题. #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<list> #include<

11462 - Age Sort

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2457 题目大意: 给出数组年龄数据,每组有n(0<n<=2000000)个数据,要求按从小到大的顺序进行排列,需要注意的是输入的数据有可能比较多,所以需要使用更快的IO 案例: Sample Input 5 3 4 2 1 5 5 2 3 2 3 1 0 Sample

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

线性排序算法---- 计数排序, 基数排序, 桶排序

排序算法里,除了比较排序算法(堆排序,归并排序,快速排序),还有一类经典的排序算法-------线性时间排序算法.听名字就让人兴奋! 线性时间排序,顾名思义,算法复杂度为线性时间O(n) , 非常快,比快速排序还要快的存在,简直逆天.下面我们来仔细看看三种逆天的线性排序算法, 计数排序,基数排序和桶排序. 1计数排序  counting Sort 计数排序 假设 n个 输入元素中的每一个都是在 0 到 k 区间内的一个整数,当 k= O(N) 时,排序运行的时间为 O(N). 计数排序的基本思想

COGS 1406. 邻居年龄排序[Age Sort,UVa 11462]

★   输入文件:AgeSort.in   输出文件:AgeSort.out   简单对比时间限制:1 s   内存限制:2 MB [题目描述] Mr.Zero(CH)喜闻乐见地得到了一台内存大大增强的 OI型 Apple Ⅱ,可以运行C,C++,和Pascal!为了炫耀这台高端的计算机,Mr.Zero决心将邻居们的年龄(0≤Age[i]≤120)统计后进行统计.但是,古董终究是古董,Mr.Zero拥有最多n个邻居(n≤2,400,000)但是计算机所能运行程序时的内存限制竟然达到了2MB.请你