UVA 11462-Age sort

题意:

  按年龄从小到大排序.

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <stack>
#include <queue>
#include <iterator>
#include <vector>

using namespace std;

#define LL long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define MAXN 10000010
#define MAXM 1000010

const int MAXN = 2000000+5;
int a[MAXN];

// 方法一:快速排序

int cmp(const void *a, const void *b)
{
    return *(int *)a - *(int *)b;
}

void qsort(int b[], int l, int r)
{
    int i, j, x;
    i = l;
    j = r;
    x = b[i];

    while(i != j)
    {
        while(b[j]>x&&j>i)
            j--;
        if(i < j)
        {
            b[i] = b[j];
            i++;
        }
        while(b[i]<x&&j>i)
            i++;
        if(i < j)
        {
            b[j] = b[i];
            j--;
        }
    }

    b[i] = x;
    qsort(b, l, j-1);
    qsort(b, i+1, r);
}

int main()
{
    int n;
    while(scanf("%d", &n)==1&&n)
    {
        int i;
        for(i = 0; i < n; i++ )
            scanf("%d", &a[i]);

        qsort(a, n, sizeof(a[0]), cmp);

        for(i = 0; i < n; i++ )
        {
            if(i != 0)
                printf(" ");
            printf("%d", a[i]);
        }

        printf("\n");
    }

    return 0;
}

//方法二:计数排序

int main()
{
    int n;
    int x;
    int i, j;
    while(scanf("%d", &n)==1&&n)
    {
        int flag = 0;
        memset(a, 0, sizeof(a));
        for(i = 1; i <= n; i++ )
        {
            scanf("%d", &x);
            a[x]++;
        }
        for(i = 1; i <= 100; i++ )  //年龄:1-100岁
            for(j = 1; j <= a[i]; j++ ) //每个年龄的人数(年龄从1岁开始)
            {
                if(flag)
                    printf(" ");
                flag = 1;
                printf("%d", i);
            }
        printf("\n");
    }
    return 0;
}
时间: 2024-10-06 05:11:16

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.

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

UVA 1016 - Silly Sort(置换分解+贪心)

UVA 1016 - Silly Sort 题目链接 题意:给定一个序列,数字都不同,每次可以交换两个数字,交换的代价为两数之和,要求出把这个序列变成递增最小代价 思路:利用置换的分解原理,可以把序列的每条循环单独考虑,对于每条循环而言,不断交换肯定每个数字至少会换到一次,再利用贪心的思想,如果每次拿循环中的最小值去置换,那么就是这个最小值会用长度-1次,而剩下的数字各一次,注意这里还有一种可能优的方法,就是先把整个序列中的最小值换到该循环中,等置换完再换出去,两种都考虑进来即可 代码: #in

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 1016 - Silly Sort(置换+贪心)

题目链接:uva 1016 - Silly Sort 题目大意:给定一个长度为n的序列,每次操作可以交换任意两个数的位置,代价为两个数的和,求最小代价,将序列排成有序的. 解题思路:给定序列根据数的大小映射成一个置换,分解置换的循环,对于每个循环中,肯定是用值最小的逐个去交换的代价最小,但是要考虑,可以将最小的值与序列中最小值交换,用它代替去交换,最后再换回来.取两种情况中最优的. #include <cstdio> #include <cstring> #include <