UVA 11462 Age Sort(计数排序法 优化输入输出)
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 n (0<n<=2000000), the total number of people. In the next line, there are n integers indicating the ages. Input is terminated with a case where n = 0. This case should not be processed.
Output
For each case, print a line with n 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(计数排序法 优化输入输出)的更多相关文章
- Uva-------(11462) Age Sort(计数排序)
B Age Sort Input: Standard Input Output: Standard Output You are given the ages (in years) of all ...
- UVa 11462 Age Sort
解题报告:给若干个居民的年龄排序,年龄的范围在1到100之间,输入的总人数在0到200W.这题要注意的输入的文件约有25MB,而内存限制为2MB,所以如果人数是像200W这样多的话,甚至都不能把它们都 ...
- counting sort 计数排序
//counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...
- 11462 Age Sort(计数排序)
内存不够用,用计数排序可以解决问题. #include<iostream> #include<cstdio> #include<cstdlib> #include& ...
- 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 ...
- 计数排序(Count Sort )与插入排序(Insert Sort)
计数排序法:计数数组适用于当前数组密集的情况.例如(2,3,5,4,2,3,3,2,5,4) 方法:先找出最大值最小值,之后统计每个数出现的次数,根据次数从小到大往数组里添加 计数排序法是一种不需要比 ...
- COGS 1406. 邻居年龄排序[Age Sort,UVa 11462](水题日常)
★ 输入文件:AgeSort.in 输出文件:AgeSort.out 简单对比时间限制:1 s 内存限制:2 MB [题目描述] Mr.Zero(CH)喜闻乐见地得到了一台内存大大增强 ...
- uva 10474 Where is the Marble? 计数排序
题目给出一系列数字,然后问哪个数字是从小到大排在第几的,重复出现算第一个. 数据范围为10000,不大,完全可以暴力,sort不会超时. 但是由于以前做比赛时也遇到这种题目,没注意看数据范围,然后暴力 ...
- CodeForces 558E(计数排序+线段树优化)
题意:一个长度为n的字符串(只包含26个小字母)有q次操作 对于每次操作 给一个区间 和k k为1把该区间的字符不降序排序 k为0把该区间的字符不升序排序 求q次操作后所得字符串 思路: 该题数据规模 ...
随机推荐
- 找到的两个php爬虫,分享一下
http://www.phpdig.net/ http://www.sphider.eu/ 虽然他们的文档我都看不太懂,甚至懒得去试一下安装,但还是留着吧,以后可能用得到吧
- HW3.27
import java.util.Scanner; public class Solution { public static void main(String[] args) { Scanner i ...
- Codeforces4D - Mysterious Present(LIS)
题目大意 给你一张宽为w,长为h的的贺卡,然后给你n个信封,每个信封宽为wi,长为hi,问你最多能在贺卡上嵌套多少个信封,如果某个信封i如果能够装在信封j里,当且仅当w[i]<w[j]& ...
- HTTP的升级产品:SPDY
一.什么是SPDY? SPDY是Google开发的基于传输控制协议(TCP)的应用层协议 .目前已经被用于Google Chrome浏览器中来访问Google的SSL加密服务.SPDY当前并不是一个标 ...
- linux文件权限查看及修改-chmod
查看linux文件的权限:ls -l 文件名称 查看linux文件夹的权限:ls -ld 文件夹名称(所在目录) 修改文件及文件夹权限: sudo chmod -(代表类型)×××(所有者)×××(组 ...
- 教程-Delphi设置功能表
1.锁定窗体上的控件,禁止移动位置 D7-Edit>Lock Controls 2.设置控件永久显示名字 D7-Tools>Environment Options>Designer& ...
- hdu4433 locker
暴力dp.. dp[i][j][k] 表示 前i位完全匹配 j 表示i+1位 k表示i+2位 枚举j k #include<iostream> #include<cstdio> ...
- [struts2]struts标签遍历map[转]
转至:http://hanxin0311.iteye.com/blog/1745792 private Map<String, String> strMap = new HashMap&l ...
- cluster模块实现多进程-让我的代理服务速度飞起来了
cluster模块实现多进程 现在的cluster已经可以说完全做到的负载均衡,在做代理服务和http服务器的时候能够讲服务器性能发挥到最大.来看一下具体的实现吧 var cluster = requ ...
- javascript-智能社-笔记
JavaScript是什么 JavaScript就是修改样式 编写JS的流程 布局:HTML+CSS 属性:确定要修改哪些属性 事件:确定用户做哪些操作(产品设计) 编写JS:在事件中,用JS来修改页 ...