topic:
2018 cattle off more school fourth field: Hash Function
Meaning of the questions:
Known hash function hash (x) = x mod n, if the hash collision occurs, the hash (x) = hash (x + 1), until there is no conflict, are now given a hash table, output lexicographically minimum insertion sequence
analysis:
If x is not the hash (x) position, then the hash [x] to the position x where all numbers must first insert the ratio x, topological sorting run readily occur to FIG construction, but directly built FIG complexity N ^ 2, because the point of the range is the period of the construction side, considering the extent map of this segment to a point of the tree, each node of the tree line and his father even a one-way side (two sons had the ability to father ), was built to find the edge when the corresponding node can be built side note: It may be illegal interval, the interval is not filled with the prefix and determine the value of the minimum lexicographical priority leaf node just fine
Code:
#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e6+255;
int tr[maxn<<2],a[maxn],sum[maxn],T,n;
struct edge{
int to,nxt;
}e[maxn<<2];
int head[maxn<<2],cnt,pos[maxn<<2],val[maxn<<2],in[maxn<<2],vis[maxn<<2],num;
inline void add(int u,int v){
e[++cnt].to = v;
e[cnt].nxt = head[u];
head[u] = cnt;
in[v]++;
}
void build(int l,int r,int x){
num++; val[x] = -1; vis[x] = 1; //num记录总结点的个数,vis标记那些节点使用了
if(l == r){
pos[l] = x;
val[x] = a[l];
return ;
}
int mid = (l+r) >> 1;
build(l,mid,x<<1);
build(mid+1,r,x<<1|1);
add(x<<1,x);add(x<<1|1,x);
}
void Updata(int l,int r,int L,int R,int x,int u){
if(L > R) return;
if(l > R || r < L) return ;
if(l >= L && r <= R){
add(x,u);
return ;
}
int mid = (l+r) >> 1;
Updata(l,mid,L,R,x<<1,u);
Updata(mid+1,r,L,R,x<<1|1,u);
}
struct node{
int x;
friend bool operator <(node a,node b){
return val[a.x] > val[b.x];
}
};
vector<int> ans;
bool spafa(){
priority_queue<node> q;
int sum = 0;
for(int i = 1;i <= (n<<2); ++i) if(!in[i]&&vis[i]) q.push(node{i});
while(!q.empty()){
node u = q.top();q.pop();
sum++;
if(~val[u.x]) ans.push_back(val[u.x]);
for(int i = head[u.x]; i;i = e[i].nxt){
int v = e[i].to;
if(--in[v] == 0) q.push(node{v});
}
}
return sum == num;
}
void init(int n){
ans.clear();cnt = num = 0;
for(int i = 0;i <= (n<<2); ++i)
head[i] = vis[i] = in[i] = 0;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
init(n);
for(int i = 1;i <= n; ++i) scanf("%d",a+i);
for(int i = 1;i <= n; ++i){
sum[i] = sum[i-1];
if(~a[i]) sum[i]++;
}
build(1,n,1); bool flag = true;
for(int i = 1;i <= n && flag; ++i){
if(~a[i]){
int tep = a[i] % n + 1;
if(i > tep) {
Updata(1,n,tep,i-1,1,pos[i]);
if(sum[i-1]-sum[tep-1] != i-tep) flag = false;
}
else if(i < tep){
Updata(1,n,tep,n,1,pos[i]);
if(sum[n]-sum[tep-1] != n-tep+1) flag = false;
Updata(1,n,1,i-1,1,pos[i]);
if(sum[i-1] != i-1) flag = false;
}
}
}
if(flag&&spafa()){
for(int i = 0;i < (int)ans.size(); ++i) printf("%d ", ans[i]);
putchar('\n');
}
else puts("-1");
}
return 0;
}