经过IDE测试,告诉我是cin>>n出错了。。。
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+10;
int n,p[N],res[N];
stack<int> t;
struct node{int v,x;}a[N];
bool operator<(node a,node b){return (a.v==b.v)?(a.v>b.v):(a.x<b.x);}
void solve(){
t=stack<int>();
cin>>n;
t.push(n+1);
for (int i=1;i<=n;i++){
cin>>p[i];
if (t.top()<=i) t.pop();
if (t.top()<p[i]){cout<<"-1\n";return;}
t.push(p[i]);
if (t.top()<=i) t.pop();
a[i]={p[i],i};
}
for (int i=1;i<=n;i++) if (p[i]!=-1&&p[p[i]]==-1) a[p[i]].v=p[i]+1;
sort(a+1,a+n+1);
for (int i=1;i<=n;i++) res[a[i].x]=n-i+1;
for (int i=1;i<=n;i++) cout<<res[i]<<' ';
cout<<'\n';
}
int main(){
int t;cin>>t;
while (t--) solve();
return 0;
}
请大佬帮忙看一看。。。