Escape Room

题目链接:http://icpc.upc.edu.cn/problem.php?cid=1849&pid=10

题目描述
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

题意:这道题的题意是让我们输出一个字典序最小序列,输出 i 后面(包括 i )的最长增加子序列的长度与a[i]相等;如输出4以及后面最长增加子序列长度为1,输出2以及后面最长增加子序列长度为2(2、3)……
思路:按照他们给出的长度从小到大排序,如果长度一样,按照下标从小到大排序;
另开一个数组储存从n到1倒序的数字,然后赋值给排序好的数组;
最后,再按照下标排序回去就可以了。

代码:

#include<bits/stdc++.h>
using namespace std;
int n,b[100010];
struct s
{
 int id,num;
}s[100010];
int cmp1(struct s a,struct s b)
{
 if(a.num==b.num) return a.id<b.id;
 return a.num<b.num;
}
int cmp2(struct s a,struct s b)
{
 return a.id<b.id;
}
int main()
{
 cin>>n;
 for(int i=1;i<=n;i++)
 {
  cin>>s[i].num;
  s[i].id=i;
  b[i]=n-i+1;
 }
 sort(s+1,s+n+1,cmp1);
 for(int i=1;i<=n;i++)
 {
  s[i].num=b[i];
 }
 sort(s+1,s+n+1,cmp2);
 cout<<s[1].num;
 for(int i=2;i<=n;i++) cout<<" "<<s[i].num;
 cout<<endl;
 return 0;
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值