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 1≤j≤i1≤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.
  • 1≤N≤1001≤N≤100
  • 1≤bi≤N1≤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=;while(b){if(b%)ans=ans*a%MOD;a=a*a%MOD;b/=;}return ans;}
inline void getInt(int* p);
const int maxn=;
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,,n)
{
cin>>a[i];
}
int isok=;
std::vector<int> ans;
int len=n;
while(len)
{
int temp=len;
for(int i=len;i>=;i--)
{
if(a[i]==i)
{
ans.push_back(i);
repd(j,i,len)
{
a[j]=a[j+];
}
len--;
break;
}
}
if(temp==len)
{
break;
}
}
if(len>)
{
cout<<-<<endl;
}else
{
reverse(ALL(ans));
for(auto x:ans)
{
cout<<x<<endl;
}
} return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

AtCoder Grand Contest 032 A - Limited Insertion( 思维)的更多相关文章

  1. Atcoder Grand Contest 032

    打的第一场Atcoder,已经知道会被虐得很惨,但没有想到竟然只做出一题-- 思维急需提升. A - Limited Insertion 这题还是很签到的. 感觉正着做不好做,我们反着来,把加数变为删 ...

  2. Atcoder Grand Contest 032 E - Modulo Pairing(乱搞+二分)

    Atcoder 题面传送门 & 洛谷题面传送门 神仙调整+乱搞题. 首先某些人(including me)一看到最大值最小就二分答案,事实上二分答案对这题正解没有任何启发. 首先将 \(a_i ...

  3. Atcoder Grand Contest 031B(DP,思维)

    #include<bits/stdc++.h>using namespace std;int a[200007];int b[200007];long long dp[200007];lo ...

  4. AtCoder Grand Contest 032 B - Balanced Neighbors——构造

    题意 B - Balanced Neighbors 给定一个整数 $N$($3\leq N \leq 100$),构造一个顶点编号为 $1...N$ 的无向图,需满足如下两个条件: 简单图且连通 存在 ...

  5. Atcoder Grand Contest 037A(贪心,思维)

    #include<bits/stdc++.h>using namespace std;string s;char ans[200007][7];char anss[200007][7];i ...

  6. AtCoder Grand Contest 019 B - Reverse and Compare【思维】

    AtCoder Grand Contest 019 B - Reverse and Compare 题意:给定字符串,可以选定任意i.j且i<=j(当然i==j时没啥卵用),然后翻转i到j的字符 ...

  7. Atcoder Grand Contest 024 E - Sequence Growing Hard(dp+思维)

    题目传送门 典型的 Atcoder 风格的计数 dp. 题目可以转化为每次在序列中插入一个 \([1,k]\) 的数,共操作 \(n\) 次,满足后一个序列的字典序严格大于前一个序列,问有多少种操作序 ...

  8. AtCoder Grand Contest 012

    AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...

  9. AtCoder Grand Contest 011

    AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...

随机推荐

  1. 虎牙直播张波:掘金Nginx日志

    大家好!我是来自虎牙直播技术保障部的张波.今天主要会从数据挖掘层面跟大家探讨一下 Nginx 的价值.OpenResty 在虎牙的应用场景主要 WAF 和流控等方面,我今天主要分享的是“ Nginx ...

  2. okhttputils【 Android 一个改善的okHttp封装库】使用(二)

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 上一篇讲了如何在项目中导入OKHttputils库的操作,这一篇主要讲常见请求的写法. get请求 public String getPe ...

  3. TabLayoutBottomDemo【TabLayout实现底部选项卡】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用TabLayout实现底部选项卡切换功能. 效果图 代码分析 1.演示固定模式的展现 2.演示自定义布局的实现 使用步骤 一.项 ...

  4. 限定项目的 Node.js 版本

    限定项目运行所需的 Node.js 版本可保证项目在一个稳定可预期的环境中运行,减少不必要的故障.甚至有些依赖库只能工作于某些版本下.同时,不加以限制的话,在多人合作的项目中恐怕会引起环境不一致带来的 ...

  5. springboot~添加新模块的方法

    在springboot项目框架里,把一个项目两大模块,主项目main和测试项目test,而我们的测试项目根据功能又可以再分,比如可以有单元测试,集成测试,业务测试等等. 对于一个初学者来说,建立模块的 ...

  6. .NET Core开发者的福音之玩转Redis的又一傻瓜式神器推荐

    作者:依乐祝 原本链接:https://www.cnblogs.com/yilezhu/p/9947905.html 引子 为什么写这篇文章呢?因为.NET Core的生态越来越好了!之前玩转.net ...

  7. RDIFramework.NET V3.3 Web版角色授权管理新增角色对操作权限项、模块起止生效日期的设置

    在实际应用在我们可能会有这样的需求,某个操作权限项(按钮)或菜单在某个时间范围内可以让指定角色访问.此时通过我们的角色权限扩展设置就可以办到. 在我们框架V3.3 Web版本全新增加了角色权限扩展设置 ...

  8. 痞子衡嵌入式:ARM Cortex-M内核MCU开发那些事 - 索引

    大家好,我是痞子衡,是正经搞技术的痞子.本系列痞子衡给大家介绍的是ARM Cortex-M内核微控制器相关知识. ARM公司从2004年开始推出Cortex-M系列内核,迄今Cortex-M家族已经包 ...

  9. DSAPI 图形图像篇(上)

    彩色文字对象 基于一些特殊需求,本人开发了彩色文字对象,该功能通过类似html代码的方式指示文本,并输出图像. 我们还是先来看一张图像. 这不是文本,是通过指定文本代码输出的图像.我们来看一下实现代码 ...

  10. 杭电ACM2019--数列有序!

    数列有序! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...