Escape Room

题目描述

As you know, escape rooms became very popular since they allow you to play the role of a video game hero. One such room has the following quiz. You know that the locker password is a permutation of N numbers. A permutation of length N is a sequence of distinct positive integers, whose values are at most N. You got the following hint regarding the password - the length of the longest increasing subsequence starting at position i equals Ai. Therefore you want to find the password using these values. As there can be several possible permutations you want to find the lexicographically smallest one. Permutation P is lexicographically smaller than permutation Q if there is an index i such that Pi < Qi and Pj = Qj for all j < i. It is guaranteed that there is at least one possible permutation satisfying the above constraints.
Can you open the door?

输入

The first line of the input contains one integer N (1≤N≤105).
The next line contains N space-separated integer Ai (1≤Ai≤N).
It’s guaranteed that at least one possible permutation exists.

输出

Print in one line the lexicographically smallest permutation that satisfies all the conditions.

样例输入
复制样例数据 4
1 2 2 1

样例输出
4 2 1 3

题意 根据所给的序列(每个数代表从这个位置开始的最长递增子序列长度)找出满足条件的字典序最小序列
找最优解 将所给序列按照从从小到大排序将,要求递增序列尽可能小就让数字尽可能大,所以将n-1依次赋给排完序后的结构体,然后将结构体按编号从小到大排即可

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
struct no {
	int num;
	int name;
}ss[100010];
int cmp1(no a, no b)
{
	return a.num < b.num;
}
int cmp2(no a, no b)
{
	return a.name < b.name;
}
int main()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> ss[i].num;
		ss[i].name = i;
		
	}
	sort(ss+1, ss + n + 1, cmp1);
	for (int i = 1; i <= n; i++)
	{
			ss[i].num = n - i + 1;
	}
	sort(ss + 1, ss + 1 + n, cmp2);
	for (int i = 1; i <= n; i++)
	{
			if(i!=n) cout << ss[i].num<<" ";
			else  cout << ss[i].num;
    }
	
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值