5140 Escape Room SEERC2017

http://exam.upc.edu.cn/problem.php?cid=1292&pid=5

题目描述

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


题目大意:输入一组数据Ai,Ai表示以下标i为起点的最大递增序列的长度为Ai,求满足输入数据的一组序列P,且保证这组序列按字典排序是最小的。
解决方法:我们把Ai与Pi一一对应,然后把Ai按从小到大排列,同时Pi也跟着排列,我们发现最终的结果满足这种处理后的Pi是从大到小排列的,而Ai是从小到大排列的
     在实现这个代码时,一开始我们就想到用sort函数对Ai进行排序,但是在写比较条件的时候,我们忘了判断当Ai中有两个相等的值的情况。主要原因在于对sort函数的不理解,
    而我们试的样例范围很小,所以我们试的都过了,但是提交后就WA
 1 #include <iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 using namespace std;
 5 struct node
 6 {
 7     int val;
 8     int pos;
 9 } lin[100050];
10 int ans[100050];
11 bool cmp(node a,node b)
12 {
13     if(a.val==b.val)
14     {
15         return a.pos<b.pos;
16     }
17     return a.val<b.val;
18 }
19 int main()
20 {
21     int n,m,i;
22     scanf("%d",&n);
23     m=n;
24     for(i=0; i<n; i++)
25     {
26         scanf("%d",&lin[i].val);
27         lin[i].pos=i;
28     }
29     sort(lin,lin+n,cmp);
30     for(i=0; i<n; i++)
31     {
32         ans[lin[i].pos]=m;
33         m--;
34     }
35     for(i=0; i<n-1; i++)
36     {
37         printf("%d ",ans[i]);
38     }
39     printf("%d\n",ans[n-1]);
40     return 0;
41 }
View Code

 

posted @ 2018-04-28 00:14  Scott_Wong  阅读(388)  评论(0编辑  收藏  举报