Time Limit: 2 sec / Memory Limit: 1024 MB
Score : <var>400400</var> points
Problem Statement
Snuke has an empty sequence <var>aa</var>.
He will perform <var>NN</var> operations on this sequence.
In the <var>ii</var>-th operation, he chooses an integer <var>jj</var> satisfying <var>1≤j≤i1≤j≤i</var>, and insert <var>jj</var> at position <var>jj</var> in <var>aa</var> (the beginning is position <var>11</var>).
You are given a sequence <var>bb</var> of length <var>NN</var>. Determine if it is possible that <var>aa</var> is equal to <var>bb</var> after <var>NN</var> operations. If it is, show one possible sequence of operations that achieves it.
Constraints
- All values in input are integers.
- <var>1≤N≤1001≤N≤100</var>
- <var>1≤bi≤N1≤bi≤N</var>
Input
Input is given from Standard Input in the following format:
<var>NN</var> <var>b1b1</var> <var>……</var> <var>bNbN</var>
Output
If there is no sequence of <var>NN</var> operations after which <var>aa</var> would be equal to <var>bb</var>, print -1
. If there is, print <var>NN</var> lines. In the <var>ii</var>-th line, the integer chosen in the <var>ii</var>-th operation should be printed. If there are multiple solutions, any of them is accepted.
Sample Input 1 Copy
3 1 2 1
Sample Output 1 Copy
1 1 2
In this sequence of operations, the sequence <var>aa</var> changes as follows:
- After the first operation: <var>(1)(1)</var>
- After the second operation: <var>(1,1)(1,1)</var>
- After the third operation: <var>(1,2,1)(1,2,1)</var>
Sample Input 2 Copy
2 2 2
Sample Output 2 Copy
-1
<var>22</var> cannot be inserted at the beginning of the sequence, so this is impossible.
Sample Input 3 Copy
9 1 1 1 2 2 1 2 3 2
Sample Output 3 Copy
1 2 2 3 1 2 2 1 1
题意:
初始你有一个空的数组,
你将执行以下操作n次,
第i次你可以选择一个1~i的数,
并把这个数插入数组的第i个位置,之前的i和i的位置如果有数将向后移动。
现在给你最后的结果数组,让你判断是否可以通过操作来完成,如果可以请输出一个方案。
思路:
我们可以用逆向思维,我们知道这n个操作的最后一个操作一定是把i放在i的位置,那么我们不妨从大到小枚举数组的a[i] 是否等于 i
如果等于,我们可以把它作为我们的最后一次操作,然后把这个数从数组中删除,然后再重复上面的操作,来找次最后的操作。。
如果某一步找不到一个a[i]==i时,那么可以得出没有方案得到这个数组。
如果都可以删除掉,然后把中途找到的数i,逆序输出,就说我们要输出的答案了。
细节见代码:
#include <iostream> #include <bits/stdc++.h> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <map> #include <set> #include <vector> #include <iomanip> #define ALL(x) (x).begin(), (x).end() #define rt return #define dll(x) scanf("%I64d",&x) #define xll(x) printf("%I64d\n",x) #define sz(a) int(a.size()) #define all(a) a.begin(), a.end() #define rep(i,x,n) for(int i=x;i<n;i++) #define repd(i,x,n) for(int i=x;i<=n;i++) #define pii pair<int,int> #define pll pair<long long ,long long> #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0) #define MS0(X) memset((X), 0, sizeof((X))) #define MSC0(X) memset((X), '\0', sizeof((X))) #define pb push_back #define mp make_pair #define fi first #define se second #define eps 1e-6 #define gg(x) getInt(&x) #define db(x) cout<<"== [ "<<x<<" ] =="<<endl; using namespace std; typedef long long ll; ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} ll lcm(ll a,ll b){return a/gcd(a,b)*b;} ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;} inline void getInt(int* p); const int maxn=1000010; const int inf=0x3f3f3f3f; /*** TEMPLATE CODE * * STARTS HERE ***/ int n; int a[maxn]; int main() { //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin); //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout); // list<int> ls; // int n; gbtb; cin>>n; repd(i,1,n) { cin>>a[i]; } int isok=0; std::vector<int> ans; int len=n; while(len) { int temp=len; for(int i=len;i>=1;i--) { if(a[i]==i) { ans.push_back(i); repd(j,i,len) { a[j]=a[j+1]; } len--; break; } } if(temp==len) { break; } } if(len>0) { cout<<-1<<endl; }else { reverse(ALL(ans)); for(auto x:ans) { cout<<x<<endl; } } return 0; } inline void getInt(int* p) { char ch; do { ch = getchar(); } while (ch == ' ' || ch == '\n'); if (ch == '-') { *p = -(getchar() - '0'); while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 - ch + '0'; } } else { *p = ch - '0'; while ((ch = getchar()) >= '0' && ch <= '9') { *p = *p * 10 + ch - '0'; } } }