A: Limited Insertion
时间限制: 1 Sec 内存限制: 128 MB
提交: 182 解决: 77
[提交] [状态] [命题人:admin]
题目描述
Snuke has an empty sequence a.
He will perform N operations on this sequence.
In the i-th operation, he chooses an integer j satisfying 1≤j≤i, and insert j at position j in a (the beginning is position 1).
You are given a sequence b of length N. Determine if it is possible that a is equal to
b after N operations. If it is, show one possible sequence of operations that achieves it.
Constraints
·All values in input are integers.
·1≤N≤100
·1≤bi≤N
输入
Input is given from Standard Input in the following format:
N
b1 … bN
输出
If there is no sequence of N operations after which a would be equal to b, print -1. If there is, print N lines. In the i-th line, the integer chosen in the i-th operation should be printed. If there are multiple solutions, any of them is accepted.
样例输入
3 1 2 1
样例输出
1 1 2
提示
In this sequence of operations, the sequence a changes as follows:
·After the first operation: (1)
·After the second operation: (1,1)
·After the third operation: (1,2,1)
仔细思考给出的操作,最后一步的操作的数,肯定是下标和数组中该位置的数相等的数,把其删去后,倒数第二步的数也相同。所以,倒着遍历数组,找下标和数组中该位置上数相等的数即可。
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 1e6+10;
int ans[110];
struct node
{
int num,index;
}a[110];
bool cmp(node a,node b)
{
return a.index<b.index;
}
int main(void)
{
int n,x;
bool flag=1,flag1;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i].num);
a[i].index=i;
}
for(int i=n;i>=1;i--)
{
flag1=0;
for(int j=i;j>=1;j--)
{
if(a[j].num==j)
{
ans[i]=j;
a[j].index+=1000;
flag1=1;
sort(a+1,a+n+1,cmp);
break;
}
}
if(!flag1)
{
flag=0;
break;
}
}
if(flag)
{
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);
}
else
printf("-1\n");
}