ACM学习历程—NPU1086 随机数 2015年陕西省程序设计竞赛网络预赛(正式赛)C题 (计数排序 || set容器)
Description
开学了,ACM队的边老板想在学校中请一些妹子一起做一项问卷调查,调查妹子们对ACM的了解情况,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的妹子的编号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。老板怎么会自己去解决这么简单的问题了,所以就请你协助完成“去重”与“排序”的工作啦。
Input
第一行是一个正整数 T ,表示这一组测试数据的总个数。
每一组的第一行包含一个数N
每一组的第2行有N个用空格隔开的正整数,为所产生的随机数。
Output
对于每一组数据,输出为2行,第1行为1个正整数M,表示不相同的随机数的个数。第2行为M个用空格隔开的正整数,为从小到大排好序的不相同的随机数。
Sample Input
2
10
20 40 32 67 40 20 89 300 400 15
3
1 2 3
Sample Output
8
15 20 32 40 67 89 300 400
3
1 2 3
这个题目描述就是一个不重复的计数排序,但是正因为如此,可以直接使用set容器。可重复的可以使用map容器。
计数排序很好理解,s[i]存的就是数字i的个数。不过需要初始化全为0。这样来一个i,s[i]就自加。
然后输出所有非0的s[i]的i,用flag控制一下空格输出。
这个算法的复杂度是O(max(n, p)),其中p为整个数列里的最大值,这里p <= 1000, n <= 100
但是使用set容器的话,只用把所有的数加入容器,然后顺序输出即可,复杂度是O(nlogn)
由于这里n比p小了一个数量级,所以第二种方法更快一点,事实证明第一种跑了2MS, 第二种跑了1MS。
代码:
计数排序版:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set> using namespace std; int s[], n, cnt, len; void Input()
{
int v;
memset(s, , sizeof(s));
cnt = len = ;
scanf("%d", &n);
for (int i = ; i < n; ++i)
{
scanf("%d", &v);
if (!s[v])
cnt++;
s[v]++;
len = max(len, v);
}
} void Output()
{
bool flag = false;
printf("%d\n", cnt);
for (int i = ; i <= len; ++i)
{
if (!s[i])
continue;
if (flag)
printf(" ");
printf("%d", i);
flag = true;
}
printf("\n");
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
Input();
Output();
}
}
set容器版:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <set> using namespace std; void Input(set <int> &s)
{
int n, v;
scanf("%d", &n);
for (int i = ; i < n; ++i)
{
scanf("%d", &v);
s.insert(v);
}
} void Output(set <int> &s)
{
bool flag = false;
printf("%d\n", s.size());
set <int> :: iterator it;
for (it = s.begin(); it != s.end(); ++it)
{
if (flag)
printf(" ");
printf("%d", *it);
flag = true;
}
printf("\n");
} int main()
{
//freopen("test.in", "r", stdin);
int T;
scanf("%d", &T);
for (int times = ; times < T; ++times)
{
set <int> s;
Input(s);
Output(s);
}
}
ACM学习历程—NPU1086 随机数 2015年陕西省程序设计竞赛网络预赛(正式赛)C题 (计数排序 || set容器)的更多相关文章
- ACM学习历程—NPU1045 2015年陕西省程序设计竞赛网络预赛(热身赛)C题 Graph Theory(递推 && 组合数学 && 大数)
Description In graph theory, a matching or independent edge set in a graph G = (V , E) is a set of e ...
- ACM学习历程—NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推)
Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m. ...
- NPU 2015年陕西省程序设计竞赛网络预赛(正式赛)F题 和谐的比赛(递推 ||卡特兰数(转化成01字符串))
Description 今天西工大举办了一场比赛总共有m+n人,但是有m人比较懒没带电脑,另外的n个人带了电脑.不幸的是,今天机房的电脑全坏了只能用带的电脑,一台电脑最多两人公用,确保n>=m. ...
- ACM学习历程——HDU5137 How Many Maos Does the Guanxi Worth(14广州10题)(单源最短路)
Problem Description "Guanxi" is a very important word in Chinese. It kind of means &quo ...
- ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...
- ACM学习历程—SNNUOJ 1110 传输网络((并查集 && 离线) || (线段树 && 时间戳))(2015陕西省大学生程序设计竞赛D题)
Description Byteland国家的网络单向传输系统可以被看成是以首都 Bytetown为中心的有向树,一开始只有Bytetown建有基站,所有其他城市的信号都是从Bytetown传输过来的 ...
- ACM学习历程—SNNUOJ 1116 A Simple Problem(递推 && 逆元 && 组合数学 && 快速幂)(2015陕西省大学生程序设计竞赛K题)
Description Assuming a finite – radius “ball” which is on an N dimension is cut with a “knife” of N- ...
- ACM学习历程—HDU5476 Explore Track of Point(平面几何)(2015上海网赛09题)
Problem Description In Geometry, the problem of track is very interesting. Because in some cases, th ...
- ACM学习历程—HDU5478 Can you find it(数论)(2015上海网赛11题)
Problem Description Given a prime number C(1≤C≤2×105), and three integers k1, b1, k2 (1≤k1,k2,b1≤109 ...
随机推荐
- 转:3.3V和5V电平双向转换——NMOS管
分简单,仅由3个电阻加一个MOS管构成,电路图如下: 此电路来自于飞利浦的一篇设计指导文档,是I2C总线官方推荐使用的电平转换电路.在实际使用过程中,需要尤其注意NMOS管的选型以及上拉电阻阻值的选取 ...
- java 接口回调
学习自:http://blog.csdn.net/xiaanming/article/details/8703708/ http://hellosure.iteye.com/blog/1130176 ...
- Android 自己定义ImageView实现圆角/圆形 附加OnTouchListener具体凝视以及Button圆角
转载请注明出处:王亟亟的大牛之路 平时要用一些非方方正正的button之类的小伙伴们是怎样实现的?RadioButton? ImageButton? 还是其它? 今天亟亟上的是ImageView来实现 ...
- python sax解析xml
#books.xml<catalog> <book isbn="0-596-00128-2"> <title>Python & XML& ...
- sed: -e expression #1, unknown option to `s'解决办法
报错如下: sed: -e expression #1, char 13: unknown option to `s' 需要替换的行为: monitor.url=http://192.168.25.1 ...
- ubuntu apt 主要命令及参数
1. apt-cache search package 搜索安装包 2. apt-cache search all 搜索所有安装包 3. apt-cache show package 显示安装包信息 ...
- PageHelper
https://pagehelper.github.io/ Mybatis分页插件PageHelper简单使用 SpringBoot之分页PageHelper
- maven工作的过程
1 建立各个module之间的依赖关系 2 越底层的依赖的module先生成 3 下载远程库中的依赖 4 先生成本地被依赖的module 问题是,如何保证本次module和远程库中的包不重名?
- PPID=1 runs as a background process, rather than being under the direct control of an interactive user
https://en.wikipedia.org/wiki/Daemon_(computing) [后台进程,非互动] d 结尾 syslogd 系统日志记录 sshd 响应ssh连接请求 In mu ...
- HNOI2017
本蒟蒻表示终于把$HNOI2017$全AC了... 万岁! 附上各个题的题解: $DAY1$: $T1$: BZOJ4825: [Hnoi2017]单旋 $T2$: BZOJ4826: [Hnoi20 ...