1. 程式人生 > >AtCoder Grand Contest 032 A - Limited Insertion( 思維)

AtCoder Grand Contest 032 A - Limited Insertion( 思維)

operation ems gcd text The tint type lse base

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 400400 points

Problem Statement

Snuke has an empty sequence aa.

He will perform NN operations on this sequence.

In the ii-th operation, he chooses an integer jj satisfying 1ji1≤j≤i, and insert jj at position jj in aa (the beginning is position

11).

You are given a sequence bb of length NN. Determine if it is possible that aa is equal to bb after NN operations. If it is, show one possible sequence of operations that achieves it.

Constraints

  • All values in input are integers.
  • 1N1001≤N≤100
  • 1biN1≤bi≤N

Input

Input is given from Standard Input in the following format:

NN
b1b1  bNbN

Output

If there is no sequence of NN operations after which aa would be equal to bb, print -1. If there is, print NN lines. In the ii-th line, the integer chosen in the ii-th operation should be printed. If there are multiple solutions, any of them is accepted.


Sample Input 1 Copy

Copy
3
1 2 1

Sample Output 1 Copy

Copy
1
1
2

In this sequence of operations, the sequence aa changes as follows:

  • After the first operation: (1)(1)
  • After the second operation: (1,1)(1,1)
  • After the third operation: (1,2,1)(1,2,1)

Sample Input 2 Copy

Copy
2
2 2

Sample Output 2 Copy

Copy
-1

22 cannot be inserted at the beginning of the sequence, so this is impossible.


Sample Input 3 Copy

Copy
9
1 1 1 2 2 1 2 3 2

Sample Output 3 Copy

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;
        }
    }
}

AtCoder Grand Contest 032 A - Limited Insertion( 思維)