#include <bits/stdc++.h>
#include "monster.h"
using namespace std;
typedef long long ll;
namespace {
int n;
int arr[1002];
int arr2[1002];
void Sort(int l, int r){
if(l>=r) return;
int m = (l+r)>>1;
Sort(l, m);
Sort(m+1, r);
for(int i=l; i<=r; i++) arr2[i] = arr[i];
for(int i=l, j=m+1, k=l; k<=r; k++){
if(i==m+1) arr[k] = arr2[j++];
else if(j==r+1) arr[k] = arr2[i++];
else if(Query(arr2[i], arr2[j]) == 0) arr[k] = arr2[i++];
else arr[k] = arr2[j++];
}
}
void calculateElse(int tmp, int st){
for(int i=st; i<n; i++){
if(Query(arr[tmp], arr[i])){
reverse(arr+tmp+1, arr+i+1);
tmp = i;
}
}
}
void Reorder(int l, int r){
if((r-l) <= 7){
int cnt[1005] = {0};
for(int i=l; i<=r; i++){
for(int j=i+1; j<=r; j++){
if(Query(arr[i], arr[j])) cnt[arr[i]]++;
else cnt[arr[j]]++;
}
}
sort(arr+l, arr+r+1, [&](int x, int y){
return cnt[x] < cnt[y];
});
if(Query(arr[l], arr[l+1]) == 0) swap(arr[l], arr[l+1]);
if(Query(arr[r], arr[r-1])) swap(arr[r], arr[r-1]);
return;
}
int comp[1005] = {0};
int cnt = 0;
int lim = r;
for(int i=l+1; i<=r; i++){
if(Query(arr[l], arr[i])) comp[i] = 1, cnt++;
if(comp[i-1] && !comp[i]){
lim = i-1;
break;
}
}
if(cnt >= 2){ /// Case 1
assert(lim >= l+3);
if(Query(arr[lim], arr[lim-2])) reverse(arr+l, arr+lim);
else reverse(arr+l, arr+lim+1);
calculateElse(lim, lim+1);
return;
}
else if(lim >= l+3){ /// Case 2
if(Query(arr[l+1], arr[lim])) reverse(arr+l+1, arr+lim+1);
else reverse(arr+l+2, arr+lim+1), swap(arr[l], arr[l+1]);
calculateElse(lim, lim+1);
}
else if(lim==1){ /// Case 3
Reorder(l+2, r);
}
else{ /// Case 4
Reorder(l+3, r);
if(Query(arr[l], arr[l+3])){
reverse(arr+l, arr+l+3);
}
else if(Query(arr[l+1], arr[l+3])){
swap(arr[l+1], arr[l+2]);
}
else{
swap(arr[l], arr[l+1]);
}
}
}
}
vector<int> Solve(int N){
n = N;
for(int i=0; i<n; i++) arr[i] = i;
random_shuffle(arr, arr+n);
Sort(0, n-1);
Reorder(0, n-1);
// for(int i=0; i<n; i++) printf("%d ", arr[i]);
// puts("");
vector<int> idx (n);
for(int i=0; i<n; i++) idx[arr[i]] = i;
return idx;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
200 KB |
Output is correct |
2 |
Correct |
1 ms |
200 KB |
Output is correct |
3 |
Correct |
1 ms |
200 KB |
Output is correct |
4 |
Correct |
1 ms |
200 KB |
Output is correct |
5 |
Correct |
1 ms |
200 KB |
Output is correct |
6 |
Correct |
1 ms |
200 KB |
Output is correct |
7 |
Correct |
1 ms |
200 KB |
Output is correct |
8 |
Correct |
2 ms |
200 KB |
Output is correct |
9 |
Correct |
1 ms |
200 KB |
Output is correct |
10 |
Correct |
3 ms |
200 KB |
Output is correct |
11 |
Correct |
1 ms |
200 KB |
Output is correct |
12 |
Correct |
2 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
200 KB |
Output is correct |
14 |
Correct |
1 ms |
200 KB |
Output is correct |
15 |
Correct |
1 ms |
200 KB |
Output is correct |
16 |
Correct |
17 ms |
200 KB |
Output is correct |
17 |
Correct |
14 ms |
284 KB |
Output is correct |
18 |
Correct |
21 ms |
200 KB |
Output is correct |
19 |
Correct |
15 ms |
200 KB |
Output is correct |
20 |
Correct |
14 ms |
300 KB |
Output is correct |
21 |
Correct |
1 ms |
200 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
200 KB |
Output is correct |
24 |
Correct |
1 ms |
200 KB |
Output is correct |
25 |
Correct |
1 ms |
200 KB |
Output is correct |
26 |
Correct |
14 ms |
200 KB |
Output is correct |
27 |
Correct |
1 ms |
200 KB |
Output is correct |
28 |
Correct |
1 ms |
200 KB |
Output is correct |
29 |
Correct |
1 ms |
200 KB |
Output is correct |
30 |
Correct |
1 ms |
200 KB |
Output is correct |
31 |
Correct |
3 ms |
200 KB |
Output is correct |
32 |
Correct |
17 ms |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
200 KB |
Output is correct |
2 |
Correct |
1 ms |
200 KB |
Output is correct |
3 |
Correct |
1 ms |
200 KB |
Output is correct |
4 |
Correct |
1 ms |
200 KB |
Output is correct |
5 |
Correct |
1 ms |
200 KB |
Output is correct |
6 |
Correct |
1 ms |
200 KB |
Output is correct |
7 |
Correct |
1 ms |
200 KB |
Output is correct |
8 |
Correct |
2 ms |
200 KB |
Output is correct |
9 |
Correct |
1 ms |
200 KB |
Output is correct |
10 |
Correct |
3 ms |
200 KB |
Output is correct |
11 |
Correct |
1 ms |
200 KB |
Output is correct |
12 |
Correct |
2 ms |
328 KB |
Output is correct |
13 |
Correct |
1 ms |
200 KB |
Output is correct |
14 |
Correct |
1 ms |
200 KB |
Output is correct |
15 |
Correct |
1 ms |
200 KB |
Output is correct |
16 |
Correct |
17 ms |
200 KB |
Output is correct |
17 |
Correct |
14 ms |
284 KB |
Output is correct |
18 |
Correct |
21 ms |
200 KB |
Output is correct |
19 |
Correct |
15 ms |
200 KB |
Output is correct |
20 |
Correct |
14 ms |
300 KB |
Output is correct |
21 |
Correct |
1 ms |
200 KB |
Output is correct |
22 |
Correct |
1 ms |
300 KB |
Output is correct |
23 |
Correct |
1 ms |
200 KB |
Output is correct |
24 |
Correct |
1 ms |
200 KB |
Output is correct |
25 |
Correct |
1 ms |
200 KB |
Output is correct |
26 |
Correct |
14 ms |
200 KB |
Output is correct |
27 |
Correct |
1 ms |
200 KB |
Output is correct |
28 |
Correct |
1 ms |
200 KB |
Output is correct |
29 |
Correct |
1 ms |
200 KB |
Output is correct |
30 |
Correct |
1 ms |
200 KB |
Output is correct |
31 |
Correct |
3 ms |
200 KB |
Output is correct |
32 |
Correct |
17 ms |
296 KB |
Output is correct |
33 |
Correct |
99 ms |
276 KB |
Output is correct |
34 |
Correct |
97 ms |
276 KB |
Output is correct |
35 |
Correct |
94 ms |
280 KB |
Output is correct |
36 |
Correct |
111 ms |
200 KB |
Output is correct |
37 |
Correct |
89 ms |
200 KB |
Output is correct |
38 |
Correct |
94 ms |
284 KB |
Output is correct |
39 |
Correct |
110 ms |
284 KB |
Output is correct |
40 |
Correct |
84 ms |
276 KB |
Output is correct |
41 |
Correct |
86 ms |
276 KB |
Output is correct |
42 |
Correct |
93 ms |
200 KB |
Output is correct |
43 |
Correct |
66 ms |
280 KB |
Output is correct |
44 |
Correct |
126 ms |
200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
200 KB |
Output is correct |
2 |
Correct |
85 ms |
280 KB |
Output is correct |
3 |
Correct |
114 ms |
284 KB |
Output is correct |
4 |
Correct |
85 ms |
280 KB |
Output is correct |
5 |
Correct |
84 ms |
200 KB |
Output is correct |
6 |
Correct |
90 ms |
276 KB |
Output is correct |
7 |
Correct |
85 ms |
276 KB |
Output is correct |
8 |
Correct |
83 ms |
280 KB |
Output is correct |
9 |
Correct |
112 ms |
292 KB |
Output is correct |
10 |
Correct |
96 ms |
284 KB |
Output is correct |
11 |
Correct |
82 ms |
288 KB |
Output is correct |
12 |
Correct |
92 ms |
320 KB |
Output is correct |
13 |
Correct |
79 ms |
200 KB |
Output is correct |
14 |
Correct |
73 ms |
200 KB |
Output is correct |
15 |
Correct |
76 ms |
320 KB |
Output is correct |
16 |
Correct |
76 ms |
296 KB |
Output is correct |
17 |
Correct |
77 ms |
200 KB |
Output is correct |
18 |
Correct |
64 ms |
288 KB |
Output is correct |