Little Gyro and Sort(第二届中国计量大学ACM程序设计竞赛个人赛)

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值