-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBerland2.c
94 lines (80 loc) · 2.35 KB
/
Berland2.c
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <stdio.h>
int main(){
int test;
scanf("%d",&test);
for(int t=0;t<test;t++){
int n;
scanf("%d",&n);
int p[n];
for(int i=0;i<n;i++){
scanf("%d",&p[i]);
}
// char str1[n+1];
// scanf("%s",str1);
char str[n+1];
scanf("%s",str);
int arr0[n][2],arr1[n][2];
int len0=0,len1=0;
for(int i=0;i<n;i++){
if(str[i]=='0'){
arr0[len0][0] = i+1;
arr0[len0][1] = p[i];
len0++;
}else if(str[i]=='1'){
arr1[len1][0] = i+1;
arr1[len1][1] = p[i];
len1++;
}
}
// for(int i=0;i<len0;i++){
// printf("%d ",arr0[i][0]);
// printf("%d\n",arr0[i][1]);
// }
// for(int i=0;i<len1;i++){
// printf("%d ",arr1[i][0]);
// printf("%d\n",arr1[i][1]);
// }
// printf("\n");
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1-i;j++){
if(arr0[j][1]>arr0[j+1][1] && i+j<len0-1){
int temp = arr0[j][1];
arr0[j][1] = arr0[j+1][1];
arr0[j+1][1] = temp;
temp = arr0[j][0];
arr0[j][0] = arr0[j+1][0];
arr0[j+1][0] = temp;
}
if(arr1[j][1]>arr1[j+1][1] && j<len1-1){
int temp = arr1[j][1];
arr1[j][1] = arr1[j+1][1];
arr1[j+1][1] = temp;
temp = arr1[i+j][0];
arr1[j][0] = arr1[j+1][0];
arr1[j+1][0] = temp;
}
}
}
int count = 1;
int q[n];
// for(int i=0;i<len0;i++){
// printf("%d ",arr0[i][0]);
// printf("%d\n",arr0[i][1]);
// }
// for(int i=0;i<len1;i++){
// printf("%d ",arr1[i][0]);
// printf("%d\n",arr1[i][1]);
// }
for(int i=0;i<len0;i++){
q[arr0[i][0]-1] = count++;
}
for(int i=0;i<len1;i++){
q[arr1[i][0]-1] = count++;
}
for(int i=0;i<n;i++){
printf("%d ",q[i]);
}
printf("\n");
}
return 0;
}