Little Gyro and Sort(第二届中国计量大学ACM程序设计竞赛个人赛)
题目来源:
链接:https://ac.nowcoder.com/acm/contest/3190/A
来源:牛客网
示例1
输入
2
5
1 4 8 3 7
6
10000 9999 9998 9997 9996 9995
输出
1 3 4 7 8
9995 9996 9997 9998 9999 10000
说明
This problem has huge input data, use scanf instead of cin to read data to avoid time limit exceed.
这题着实比较坑,看见备注,我以为是给签到的,直接调用sort,都准备叉掉页面了,显示超时。于是我觉得可能要写快排,于是乎,又写了一波快排排序,搞心态的是,又超时。几乎接近自闭ing。最后才明白,这道题,用个for循环就可以解决。O(n)时间复杂度。
代码如下,思路看代码即可理解。
#include <iostream>
#include <cstdio>
#include <cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
int T;
int a[100000];
scanf("%d", &T);
while (T--)
{
memset(a, 0, sizeof(a));
int n;
int left = 100001;
int right = 0;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
int nn;
scanf("%d", &nn);
a[nn]++;
if (left > nn)
left = nn;
if (nn > right)
right = nn;
}
printf("%d", left);
a[left]--;
for (int i = left; i <= right; i++)
{
if (a[i] >= 1)
{
while (a[i]--)
printf(" %d", i);
}
}
cout << endl;
}
return 0;
}