字典序——思维

链接:https://ac.nowcoder.com/acm/contest/5757/I
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
小明遇到了一个问题希望你能帮他解决
现在有n个数字排成一列构成数组A,数组A中存在n个数a[i], 其中1<=i<=n。
数组sj为删除数组A中的第j个数后,剩余n-1个数构成的数组,其中1<=j<=n。
小明希望你把s1~sn的数组按照字典序大小排列起来,
若两个数组相等,则认为删除元素编号小的数组字典序更小

输入描述:
输入数据第一行是t,表示数据的组数,接下来每组数据输入n,接下来一行
一共n个数,a[i]表示数组的第i个数
(t<=10,n <= 1e5,a[i] <= 1e9)

输出描述:
输出一行n个整数,b1,b2…bn,表示Sb1 < Sb2 < … < Sbn
示例1
输入

1
7
1 1 2 1 1 1 2
输出

3 7 4 5 6 1 2

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
int t, n, tot;
const int maxn=1e5+5;

int a[maxn];
int  b[maxn];
int ans[maxn];
int main()
{
    scanf("%d", &t);
    while(t--)
    {
        scanf("%d", &n);
        tot = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &a[i]);
            b[i] = 0;
        }
        for(int i = 1; i < n; i++)
        {
            if(a[i] > a[i + 1])
            {
                int temp = i;
                while(a[temp - 1] == a[temp] && temp > 1) temp--;
                for(int j = temp; j <= i; j++)
                {
                    ans[++tot] = j;
                    b[j] = 1;
                }
            }
        }
//          for(int i = 1; i <= n; i++)
//        {
//            printf("%d", ans[i]);
//            if(i!=n) printf(" ");
//        }
        for(int i = n; i >= 1; i--)
        {
            if(!b[i])
            {
                int temp = i;
                while(a[temp - 1] == a[temp] && temp > 1) temp--;
                for(int j = temp; j <= i; j++)
                {
                    ans[++tot] = j;
                    b[j] = 1;
                }
            }
        }

        for(int i = 1; i <= n; i++)
        {
            printf("%d", ans[i]);
            if(i!=n) printf(" ");
        }
        printf("\n");
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值