这道题其实只要解决了什么时候输出 -1 ,那么此题的构造方法也就解决了。首先我们可以观察这组 3 3 4 和 3 4 4 ,可以算出第二组是不成立的,在观察一组 2 3 4 5 和  3 2 4 5 ,第二组也是不成立的。我们将 i 和 a[ i ] 视为区间两端点,可以发现只有在任意一对区间不在端点处相交时才会出现 -1 。假设 a[ i-1 ] = x , a[ i ] = y , x < y ,那么可以得出 ans[ i - 1 ] < x , ans[ i ] < y 。若 ans [ i - 1 ] < ans [ i ] ,那么 a[ i - 1 ] 应该等于 i 才对; 若 ans[ i - 1] > ans [ i ] , 又因为 a[ i - 1 ]  < x ,那么 a[ i ] 应该等于 x才对,综上所以不成立。接下来考虑 a[ i ]==-1 的时候, 那么此时只需要将 a[ i ] = i + 1 即可,这样保证与之前的区间不在除端点处之外的地方相交了,那么对于这些合法的区间,一定可以构成合法的表达式,那么我们将区间右端点向左端点引出一条边,从 n+1 开始拓扑排序,然后按照当前点连接的所有点按照端点值从小到大以此给大到小的值,这样保证任意两端点不会有额外的一条边产生,跑完后即是合法表达式。

 //      ——By DD_BOND 

 //#include<bits/stdc++.h>
#include<functional>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<iomanip>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<cstddef>
#include<cstdio>
#include<memory>
#include<vector>
#include<cctype>
#include<string>
#include<cmath>
#include<queue>
#include<deque>
#include<ctime>
#include<stack>
#include<map>
#include<set> #define fi first
#define se second
#define MP make_pair
#define pb push_back
#define INF 0x3f3f3f3f
#define pi 3.1415926535898
#define lowbit(a) (a&(-a))
#define lson l,(l+r)/2,rt<<1
#define rson (l+r)/2+1,r,rt<<1|1
#define Min(a,b,c) min(a,min(b,c))
#define Max(a,b,c) max(a,max(b,c))
#define debug(x) cerr<<#x<<"="<<x<<"\n"; using namespace std; typedef long long ll;
typedef pair<int,int> P;
typedef pair<ll,ll> Pll;
typedef unsigned long long ull; const int seed=;
const ll LLMAX=2e18;
const int MOD=1e9+;
const double eps=1e-;
const int MAXN=1e6+;
const int hmod1=0x48E2DCE7;
const int hmod2=0x60000005; inline ll sqr(ll x){ return x*x; }
inline int sqr(int x){ return x*x; }
inline double sqr(double x){ return x*x; }
ll gcd(ll a,ll b){ return b==? a: gcd(b,a%b); }
ll exgcd(ll a,ll b,ll &x,ll &y){ ll d; (b==? (x=,y=,d=a): (d=exgcd(b,a%b,y,x),y-=a/b*x)); return d; }
ll qpow(ll a,ll n){ll sum=;while(n){if(n&)sum=sum*a%MOD;a=a*a%MOD;n>>=;}return sum;}
inline int dcmp(double x){ if(fabs(x)<eps) return ; return (x>? : -); } vector<int>G[MAXN];
int a[MAXN],ans[MAXN]; int main(void)
{
ios::sync_with_stdio(false); cin.tie(); cout.tie();
int T; cin>>T;
while(T--){
set<int>s;
int n,flag=; cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
for(int i=;i<=n;i++){
if(a[i]==-) a[i]=i+;
while(s.find(i)!=s.end()) s.erase(i);
if(!s.empty()&&*s.begin()<a[i]) flag=;
s.insert(a[i]);
G[a[i]].pb(i);
}
if(flag) cout<<-<<endl;
else{
int id=n;
queue<int>q; q.push(n+);
while(!q.empty()){
int u=q.front(); q.pop();
for(int i=;i<(int)G[u].size();i++){
int v=G[u][i];
ans[v]=id--;
q.push(v);
}
}
for(int i=;i<=n;i++) cout<<ans[i]<<' ';
cout<<endl;
}
for(int i=;i<=n+;i++) G[i].clear();
}
return ;
}

Codeforces 1159E Permutation recovery(构造+拓扑)的更多相关文章

  1. Codeforces 1158C Permutation recovery

    https://codeforces.com/contest/1158/problem/C 题目 已知 $p_1, p_2, \dots, p_n$ 是 $1$ 到 $n$ 的一个排列. 给出关于这个 ...

  2. hdoj 2404 Permutation Recovery【逆序对】

    Permutation Recovery Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  3. Permutation Recovery(模拟)

    Permutation Recovery Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. 贪心 CodeForces 137B Permutation

    题目传送门 /* 贪心:因为可以任意修改,所以答案是没有出现过的数字的个数 */ #include <cstdio> #include <algorithm> #include ...

  5. Codeforces Round #275 (Div. 2) C - Diverse Permutation (构造)

    题目链接:Codeforces Round #275 (Div. 2) C - Diverse Permutation 题意:一串排列1~n.求一个序列当中相邻两项差的绝对值的个数(指绝对值不同的个数 ...

  6. Codeforces 804E The same permutation(构造)

    [题目链接] http://codeforces.com/contest/804/problem/E [题目大意] 给出一个1到n的排列,问每两个位置都进行一次交换最终排列不变是否可能, 如果可能输出 ...

  7. Codeforces 1159E(拓扑序、思路)

    要点 序列上各位置之间的关系常用连边的手段转化为图的问题. 经过一番举例探索不难发现当存在两条有向边交叉时是非法的. -1是模糊的,也就是填多少都可以,那为了尽量避免交叉我们贪心地让它后面那个连它就行 ...

  8. Codeforces 1159E 拓扑排序

    题意及思路:https://www.cnblogs.com/dd-bond/p/10859864.html 代码: #include <bits/stdc++.h> #define LL ...

  9. codeforces B. Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/359/B 题目意思:给定n和k的值,需要构造一条长度为2n(每个元素取值范围只能是[1,2n])且元素各不 ...

随机推荐

  1. bzoj3011 [Usaco2012 Dec]Running Away From the Barn 左偏树

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3011 题解 复习一下左偏树板子. 看完题目就知道是左偏树了. 结果这个板子还调了好久. 大概已 ...

  2. DevExpress v19.1新版亮点——WinForms篇(二)

    行业领先的.NET界面控件DevExpress v19.1终于正式发布,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WinForms v19.1中新增的一些控 ...

  3. PHP环境安全性能检查

    PHP环境安全性能检查 PHP在Linux环境下安全配置是一个复杂的过程,其中涉及到很多的细节设置,在这里发出来一个脚本,通过这个脚本来检测你的PHP环境是否存在安全隐患,从而针对这些对你的PHP环境 ...

  4. Mac下Sublime text3无法安装Package Control及中文乱码问题

    sublime text3是一款轻量级的代码编辑器,我曾在Windows下配置过,但时间久了就忘了.这次是在mac上配置,在网上查了一些帖子,有的叙述不是很清楚,故记录一下详细过程. 在线安装: ht ...

  5. django快速搭建blog

    python版本:3.5.4: Django版本:2.0 创建项目 创建mysite项目和 blog应用: django-admin startproject mysite # 创建mysite项目 ...

  6. Element ui 中的表单提交按钮多次点击bug修复

  7. Python用MyQR生成自定义个性二维码

    MyQR是一个能够生成自定义二维码的python第三方库,根据需要能够生成普通二维码.带背景图片的艺术二维码.动态二维码. 1.MyQR安装 安装非常简单,直接用pip install MyQR,需要 ...

  8. [CSP-S模拟测试]:集合论(模拟)

    题目传送门(内部题73) 输入格式 输入文件$jihe.in$ 第一行一个整数$m$,表示操作的次数. 接下来$m$行,每行描述一个操作. 每行的开始都是一个数字,$1,2,3,4$依次代表$unio ...

  9. 北风设计模式课程---开放封闭原则(Open Closed Principle)

    北风设计模式课程---开放封闭原则(Open Closed Principle) 一.总结 一句话总结: 抽象是开放封闭原则的关键. 1."所有的成员变量都应该设置为私有(Private)& ...

  10. Linux 下wdcp支持两种安装方式

    wdcp支持两种安装方式1 源码编译 此安装比较麻烦和耗时,一般是20分钟至一个小时不等,具体视机器配置情况而定2 RPM包安装 简单快速,下载快的话,几分钟就可以完成源码安装(ssh登录服务器,执行 ...