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.

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

【分析】

由于数据太大,内存限制太紧(甚至都不能把它们全读进内存),因此无法使用快速排序方法。但整数范围很小,可以用计数排序方法。

【代码】

/*********************************
*   日期:2014-5-2
*   作者:SJF0115
*   题号: 11462 - Age Sort
*   地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=26&page=show_problem&problem=2457
*   来源:UVA
*   结果:Accepted
*   总结:计数排序
**********************************/
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main(){
    int i,j,age,n;
    int count[101];
    //freopen("C:\\Users\\wt\\Desktop\\acm.txt","r",stdin);
    while(scanf("%d",&n)!= EOF && n != 0){
        //初始化
        memset(count,0,sizeof(count));
        //统计人数
        for(i = 0;i < n;i++){
            scanf("%d",&age);
            count[age]++;
        }
        //按照年龄从小到大输出
        bool first = true;//标志 控制格式  第一次输出
        for(i = 1;i < 101;i++){
            for(j = 0;j < count[i];j++){
                if(!first){
                    printf(" ");
                }
                first = false;
                printf("%d",i);
            }
        }
        printf("\n");
    }
    return 0;
}

如果还要精益求精,可以优化输入输出,进一步降低运行时间。程序如下。

/*********************************
*   日期:2014-5-2
*   作者:SJF0115
*   题号: 11462 - Age Sort
*   地址:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=26&page=show_problem&problem=2457
*   来源:UVA
*   结果:Accepted
*   总结:
**********************************/
#include<cstdio>
#include<cstring>
#include<cctype> 	//为了使用isdigit宏
//内联函数
//逐字符输入
inline int ReadInt(){
    char c = getchar();
    while(!isdigit(c)){
        c = getchar();
    }

    int x = 0;
    while(isdigit(c)){
        x = x * 10 + c - ‘0‘;
        c = getchar();
    }
    return x;
}
//声明成全局变量可以减小开销
int buf[10];
//逐字符输出
inline void WriteInt(int i){
    int p = 0;
    //特殊情况:i等于0的时候需要输出0,而不是什么也不输出
    if(i == 0){
        p++;
    }
    else{
        //分解为字符
        while(i){
            buf[p++] = i % 10;
            i /= 10;
        }
    }
    //逐字符输出
    for(int j = p-1; j >=0; j--){
        //逆序输出
        putchar(‘0‘ + buf[j]);
    }
}

int main() {
    int n, x, c[101];
    while(n = ReadInt()){
        memset(c, 0, sizeof(c));
        for(int i = 0; i < n; i++) c[ReadInt()]++;
        //输出
        int first = 1;
        for(int i = 1; i <= 100; i++){
            for(int j = 0; j < c[i]; j++) {
                if(!first) putchar(‘ ‘);
                first = 0;
                WriteInt(i);
            }
        }
        putchar(‘\n‘);
    }//while
    return 0;
}

上述优化使得运行时间缩短了约2/3。一般情况下,当输入输出数据量很大时,应尽量用scanf和printf函数;如果时间效率还不够高,应逐字符输入输出,就像上面的readint和writeint函数。不管怎样,在确信I/O时间成为整个程序性能瓶颈之前,不要盲目优化。测试方法也很简单:输入之后不执行主算法,直接输出一个任意的结果,看看运行时间是否过长。

UVA之11462 - Age Sort

时间: 2024-12-18 03:22:44

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

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

11462 Age Sort(计数排序)

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

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.请你

ACM Age Sort排序

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

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 si

Uva 642 - Word Amalgamation sort qsort

 Word Amalgamation  In millions of newspapers across the United States there is a word game called Jumble. The object of this game is to solve a riddle, but in order to find the letters that appear in the answer it is necessary to unscramble four wor