这道题其实只要解决了什么时候输出 -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. Bloom Filter的算法

     Bloom Filter的算法: 为了降低冲突的概念,Bloom Filter使用了多个哈希函数,而不是一个.创建一个m位BitSet,先将所有位初始化为0,然后选择k个不同的哈希函数.第i个哈希函 ...

  2. Golang在京东列表页实践总结

    Golang在京东列表页实践总结 作者:张洪涛 10余年软件开发和设计经验,曾就职于搜狐.搜狗.前matrixjoy公司联合创始人.甘普科技CTO. 目前线上状态 基于搜索实现: 全量数据,搜索结果不 ...

  3. WEB编程 入门简单 进阶难

    其实不论是WEB还是其他什么编程,都是这个道理,至于为什么,我贴几段代码,大家感受下. JS 计算今天是星期几 入门级 // 计算系统当前是星期几 var str =""; var ...

  4. Android 快速索引(城市列表和联系人)

    最近需要实现一个城市列表的快速索引功能.类似于联系人应用,根据姓名首字母快速索引功能. 要实现这个功能只需要解决两个问题:1.对列表进行分组(具有同一特征),并且能够快速定位到该组的第一项 2.右侧分 ...

  5. (3.2)狄泰软件学院C++课程学习剖析三

    对课程前面40课的详细回顾分析(一) 0. int main() { // ① Array t(3,3); //普通模式 // ② Array *t=new Array(3,3); //指针方式 // ...

  6. Redis 序列化方式StringRedisSerializer、FastJsonRedisSerializer和KryoRedisSerializer

    当我们的数据存储到Redis的时候,我们的键(key)和值(value)都是通过Spring提供的Serializer序列化到数据库的.RedisTemplate默认使用的是JdkSerializat ...

  7. Android CPU使用率:top和dump cpuinfo的不同

    CPU是系统非常重要的资源,在Android中,查看CPU使用情况,可以使用top命令和dump cpuinfo.我记得很久以前,就发现这两者存在不同,初步猜测应该是算法上存在差异.最近需要采集应用C ...

  8. zabbix主动、被动TCP连接过程

    zabbix主动.被动TCP连接过程 https://blog.csdn.net/u010668387/article/details/79460183

  9. element-ui的rules全局验证

    原文:https://www.jianshu.com/p/6a29e9e51b61 rules.js var QQV = (rule, value, callback) => { debugge ...

  10. find查找特殊权限用法

    find查找特殊权限的用法 find選項與參數: 3. 與檔案權限及名稱有關的參數: -name filename:搜尋檔案名稱為 filename 的檔案: -size [+-]SIZE:搜尋比 S ...