-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationE.java
More file actions
59 lines (55 loc) · 1.72 KB
/
Copy pathPermutationE.java
File metadata and controls
59 lines (55 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.util.*;
public class PermutationE{
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
String ts=sc.next();
if(ts==null)return;
int t=Integer.parseInt(ts);
while(t-->0){
int n=sc.nextInt();
int[]p=new int[n+1];
Integer[]order=new Integer[n];
for(int i=1;i<=n;i++){
p[i]=sc.nextInt();
order[i-1]=i;
}
int []d=new int[n+1];
for(int i=1;i<=n;i++)d[i]=sc.nextInt();
Arrays.sort(order,(a,b)-> Integer.compare(p[b],p[a]));
List<Integer>ans =new ArrayList<>();
boolean pos=true;
for(int x:order){
int target=d[x];
int count=0;
int insertPos=ans.size();
for(int i=ans.size()-1;i>=0;i--){
if(target==0)break;
if(ans.get(i)>x){
count++;
if(count==target){
insertPos=i;
break;
}
}
}
if(count<target){
pos=false;
break;
}
ans.add(insertPos,x);
}
if(!pos){
System.out.println("-1");
}else{
int []q=new int[n+1];
for(int i=0;i<ans.size();i++){
q[ans.get(i)]=i+1;
}
for(int i=1;i<=n;i++){
System.out.print(q[i]+(i==n ? "" : " "));
}
System.out.println();
}
}
}
}