2023年中国大学生程序设计竞赛女生专场,F. 最长上升子序列,思维

F. 最长上升子序列

Problem - F - Codeforces 

time limit per test

2.0 s

memory limit per test

256 megabytes

input

standard input

output

standard output

你原本有一个 1 到 n 的排列,但是不慎地,你遗忘了它,但是你记得以 第i个位置 结尾的最长上升子序列的长度数组 {an} ,现在希望你能够构造一个符合条件的排列 p ,如果不存在符合上述条件的排列 p ,则输出  - 1。

这里定义以 第i位置 结尾的最长上升子序列的长度,为符合以下条件的整数数组 中 k 的最大值。

本题输入输出量比较大,请选手注意。

Input

第一行一个整数 n (1 ≤ n ≤ 106)

第二行 n 个整数表示数组 {an} (1 ≤ ai ≤ n),其中 ai 表示以 i 结尾的最长上升子序列的长度。

Output

一行 n 个整数表示排列 p ,如果无解,则输出  - 1。

Examples

input

Copy

5
1 2 2 3 3

output

Copy

1 5 2 4 3 

input

Copy

7
1 1 2 1 4 4 4

output

Copy

-1
​​​​

解析 :

根据样例和自己造的数据发现:相同值对应元素按逆序填充是满足条件的。

#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<math.h>
#include<map>
#include<iostream>
using namespace std;
typedef long long LL;
const int N = 1e6 + 5;
int n;
int a[N];
vector<int>g[N];

int main() {
	scanf("%d", &n);
	int mx = 0;
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		if (a[i] > mx + 1) {
			cout << -1 << endl;
			return 0;
		}
		mx = max(mx, a[i]);
		g[a[i] - 1].push_back(i-1);
	}
	vector<int>ans(n);
	int x = 1;
	for (int i = 0; i < mx; i++) {
		for (int j = g[i].size() - 1; j >= 0; j--) {
			ans[g[i][j]]=x++;
		}
	}
	for (int i = 0; i < ans.size(); i++)
		printf("%d ", ans[i]);
	printf("\n");
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值