UVA之11462 - Age Sort

2024-02-19 03:32
文章标签 sort uva age 11462

本文主要是介绍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');}//whilereturn 0;
}

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


这篇关于UVA之11462 - Age Sort的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/723365

相关文章

UVA - 12206 Stammering Aliens (hash)

这题其实很容易想到2分长度,关键是2分后,怎么判断出现最多次的串是否》=m次了。这里需要用到hash来处理。 对于一个字符串   H[i] =H[i+1]*131+s[i]  ;其中 H[n]=0;那么对于一个长度为L的串 ,hanh[i,l]=H[i]-H[i+L]*x^L ; 这样记录每个字符串的hash值,然后再处理起来就比较简单了。 VIEW CODE #include<

常用命令: sort学习笔记

本文的sort命令是GNU版本(8.22), 和BSD的sort不同 sort是我最常用Linux命令之一,它的功能就是排序,一般后面还会和uniq搭配,对数据进行去重。 下面的操作假设你有一个文件,叫做chr.txt, 内容如下, 不同列之间用制表符分隔 Chr3 20251812 20254323 +Chr1 471971 473336 -Chr3

[Uva 11990] Dynamic Inversion (CDQ分治入门)

Uva - 11990 动态逆序对,求删除一个点之前序列中逆序对的个数 首先倒过来看这个问题,把点先全部删掉 然后从最后一个点开始倒着往数列中加点 求加完这个点逆序对的变化 CDQ分治做法 把删除时间看作 t,下标看作 x,值看作 y 然后对 x排序,对 t偏序,用树状数组维护 y 具体来说就是对于每个点 (t0,x0,y0) (t_0, x_0, y_0) 先统计

[Codeforces 451B] Sort the Array (实现)

Codeforces - 451B 给定一个序列,其中每个数都不相同 问是否能在翻转其中一段后,整个序列变得单调递增 实现题 首先设一个 B B数组为 AA数组排序后的结果 由于只能翻转一个区间,那么我假装 A是满足要求的 找到最小的 A[l]≠B[l] A[l] \ne B[l],最大的 A[r]≠B[r] A[r] \ne B[r], 翻转的区间将会是 [l,r

Age and gender estimation based on Convolutional Neural Network and TensorFlow

训练数据处理 imdb数据提取 gender: 0 for female and 1 for male, NaN if unknown age: 年龄分为101类,分别为从0到100岁. 将训练数据转换为tfrecords格式,命令为, python convert_to_records_multiCPU.py --imdb --nworks 8 --imdb_db /home/rese

【LeetCode】Sort Colors 数组排序

题目:Sort color <span style="font-size:18px;">/*LeetCode sort colors题目:输入一个数组,包含0,1,2分别代表红白蓝三种颜色,要求按照0,1,2的顺序,将同类颜色的连续排列思路:计数排序,是一个遍历两遍的方法:可以先统计每种的数量,之后直接将这一范围内的所有值都赋值为相应的数字即可遍历一遍的话可以在遍历的同时分别与0

UVA 11624 搜索

给出1000*1000矩阵,含起点‘J’,路‘.',墙‘#’,火‘F'; 火每秒蔓延一格,人每秒走一步 问人是否可以安全走出矩阵,不能被火碰到 先对所有火BFS一遍,记录每个点被烧到的时间 然后对人BFS一遍,若到每点前没被火烧即可走 #include "stdio.h"#include "string.h"#include "queue"using namespace

UVa 11361 Investigating Div-Sum Property

这道题居然提交了十次才过....期间小问题不断。思路的话基本是《训练指南》里面来的,不过有几个小问题需要注意一下。第一,当K在大于100的情况下,就直接输出0就可以了。因为a,b不超过2^31,可以估算出a,b最多十位十进制数,那么每位最大为9,所以各个数字之和是不可能超过100的,那么个数字之和为模K为0的条件是永远不可能到达的。       还有一点是,当剩余数字d=0时,当且

UVa 1362(LA 3516) Exploring Pyramids

依旧是《训练指南》上的一道例题。思路大致相同,即设有一个序列S(i),S(i+1),S(i+2)...S(j),d[i,j]为所求的解。当S(i)==S(k),i<k<=j 时,说明在k回到根,那么S(i+1)...S(k-1)构成一棵独立的子树(当然也可能并不是子树)。那么d[i,j]就要加上d[i+1,k-1]*d[k,j],不断递增k,每遇到一个k,d[i,j]+=d[i

UVa 11174 Stand in a Line

依旧是《训练指南》上的一道例题。书上讲的比较抽象,下面就把解法具体一下。因为涉及到父子关系,因此自然而然可以将n个节点构造成一棵树,最后将形成一个森林。接下来将使用递归的手法。设f(i)是以节点i为树根的子树,节点i有儿子c1,c2,c3....cj共j棵子树。s[i]为树根为i的子树包含的节点数。如果分别先给各个子树内部排序,那么毫无疑问, 共有f(c1)*f(c2)*f(c3).