问题 A
***:
Limited Insertion
题目描述
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>
using namespace std;
typedef long long ll;
const int N=100;
struct code
{
int num;
int dis;
int pos;
}a[N+5];
int b[N+5];
int c[N+5];
int main()
{
int x;
cin>>x;
for(int i=1;i<=x;i++)
{
scanf("%d",&a[i].num);
a[i].pos=i;
}
for(int i=1;i<=x;i++)
{
a[i].dis=0;
/*printf("%d\n",a[i].pos-a[i].dis);*/
}
int n=0,flag;
for(int i=0;i<x;i++)
{
flag=0;
for(int j=x;j>0;j--)
{
/*cout<<a[j].pos-a[j].dis<<endl;
cout<<a[j].num<<endl;*/
if(a[j].num==a[j].pos-a[j].dis&&c[j]==0)
{
b[n++]=a[j].num;
c[j]=1;
flag=1;
for(int k=j+1;k<=x;k++)
{
a[k].dis++;
}
break;
}
}
if(flag==0)
{
break;
}
}
if(flag==0)
{
printf("-1\n");
}
else
{
for(int i=x-1;i>=0;i--)
{
printf("%d\n",b[i]);
}
}
return 0;
}