#include "fun.h"
#include <bits/stdc++.h>
using namespace std;
#define S(A) sort(A.begin(),A.end())
typedef vector<pair<int,int>> VP;
vector<int>ans;
#define PB push_back
void makedouble(VP a,VP b){
S(b),S(a);
while(a.size()||b.size()){
ans.PB(b.back().second);
b.pop_back();
swap(a,b);
}
}
VP concat(VP a,VP b){
for(auto i:b)
a.PB(i);
return a;
}
void stuff(VP W[3],int a,int b,int c,int k){
if(k==a) makedouble(W[a],concat(W[b],W[c]));
else if(W[b+c-k].back().first>W[k].back().first)
makedouble(W[a],concat(W[b],W[c]));
else makedouble(concat(W[b],W[c]),W[a]);
}
vector<int> createFunTour(int N, int Q) {
ans.clear();
if(N==2) return{0,1};
pair<int,int>centroid{N,0};
for(int i=1;i<N;i++) {
int k=attractionsBehind(0,i);
if(k>=(N+1)/2)
centroid=min(centroid,{k,i});
}
int k=centroid.second;
vector<int>subtreez;
VP W[3];
for(int i=0;i<N;i++){
if(i==k) continue;
int C=hoursRequired(k,i);
if(!subtreez.size()){
subtreez.PB(i);
W[0].PB({C,i});
} else if(subtreez.size()==1){
if(W[0][0].first+C-hoursRequired(W[0][0].second,i))
W[0].PB({C,i});
else subtreez.PB(i),W[1].PB({C,i});
} else {
if(W[0][0].first+C-hoursRequired(W[0][0].second,i))
W[0].PB({C,i});
else if(W[1][0].first+C-hoursRequired(W[1][0].second,i))
W[1].PB({C,i});
else W[2].PB({C,i});
}
}
S(W[0]),S(W[1]),S(W[2]);
sort(W,W+3,[](VP &a,VP &b){
return a.size()>b.size();
});
if(W[2].empty()){
makedouble(W[1],W[0]);
} else if(W[0].size()>=W[1].size()+W[2].size()){
makedouble(concat(W[1],W[2]), W[0]);
} else {
sort(W,W+3,[](VP &a,VP &b){
return a.back()>b.back();
});
int prv=0;
ans.PB(W[0].back().second);
W[0].pop_back();
while(1){
if(ans.size()==119){
cerr<<"ohaiyo\n";
}
int A=W[0].size(),B=W[1].size(),C=W[2].size();
if(A+B==C){
stuff(W,2,0,1,prv);
break;
} else if (A+C==B){
stuff(W,1,2,0,prv);
break;
} else if (B+C==A){
stuff(W,0,1,2,prv);
break;
}
pair<int,int>s{0,0};
for(int i=0;i<3;i++)if(i-prv)
s=max(s,{W[i].back().first,i});
prv=s.second;
ans.PB(W[prv].back().second);
W[prv].pop_back();
}
}
ans.PB(k);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
436 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
436 KB |
Output is correct |
18 |
Correct |
78 ms |
16584 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
860 KB |
Output is correct |
22 |
Correct |
4 ms |
1372 KB |
Output is correct |
23 |
Correct |
7 ms |
2396 KB |
Output is correct |
24 |
Correct |
9 ms |
3260 KB |
Output is correct |
25 |
Correct |
46 ms |
9648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
6 ms |
1848 KB |
Output is correct |
15 |
Correct |
89 ms |
16056 KB |
Output is correct |
16 |
Correct |
91 ms |
15952 KB |
Output is correct |
17 |
Correct |
15 ms |
3932 KB |
Output is correct |
18 |
Correct |
38 ms |
7456 KB |
Output is correct |
19 |
Correct |
71 ms |
12708 KB |
Output is correct |
20 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
436 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
0 ms |
348 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
0 ms |
436 KB |
Output is correct |
44 |
Correct |
78 ms |
16584 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
2 ms |
860 KB |
Output is correct |
48 |
Correct |
4 ms |
1372 KB |
Output is correct |
49 |
Correct |
7 ms |
2396 KB |
Output is correct |
50 |
Correct |
9 ms |
3260 KB |
Output is correct |
51 |
Correct |
46 ms |
9648 KB |
Output is correct |
52 |
Correct |
1 ms |
348 KB |
Output is correct |
53 |
Correct |
6 ms |
1848 KB |
Output is correct |
54 |
Correct |
89 ms |
16056 KB |
Output is correct |
55 |
Correct |
91 ms |
15952 KB |
Output is correct |
56 |
Correct |
15 ms |
3932 KB |
Output is correct |
57 |
Correct |
38 ms |
7456 KB |
Output is correct |
58 |
Correct |
71 ms |
12708 KB |
Output is correct |
59 |
Correct |
3 ms |
860 KB |
Output is correct |
60 |
Correct |
112 ms |
16884 KB |
Output is correct |
61 |
Correct |
130 ms |
19408 KB |
Output is correct |
62 |
Correct |
117 ms |
16852 KB |
Output is correct |
63 |
Correct |
119 ms |
20776 KB |
Output is correct |
64 |
Correct |
125 ms |
20768 KB |
Output is correct |
65 |
Correct |
107 ms |
17096 KB |
Output is correct |
66 |
Correct |
125 ms |
19652 KB |
Output is correct |
67 |
Correct |
129 ms |
20852 KB |
Output is correct |
68 |
Correct |
128 ms |
20128 KB |
Output is correct |
69 |
Correct |
149 ms |
21708 KB |
Output is correct |
70 |
Correct |
107 ms |
16576 KB |
Output is correct |
71 |
Correct |
138 ms |
20172 KB |
Output is correct |
72 |
Correct |
116 ms |
20176 KB |
Output is correct |
73 |
Correct |
127 ms |
20172 KB |
Output is correct |
74 |
Correct |
141 ms |
21032 KB |
Output is correct |
75 |
Correct |
114 ms |
17324 KB |
Output is correct |
76 |
Correct |
123 ms |
20812 KB |
Output is correct |
77 |
Correct |
130 ms |
21084 KB |
Output is correct |
78 |
Correct |
114 ms |
20428 KB |
Output is correct |
79 |
Correct |
135 ms |
21452 KB |
Output is correct |
80 |
Correct |
131 ms |
21960 KB |
Output is correct |
81 |
Correct |
108 ms |
16028 KB |
Output is correct |
82 |
Correct |
105 ms |
16524 KB |
Output is correct |
83 |
Correct |
117 ms |
16180 KB |
Output is correct |
84 |
Correct |
43 ms |
7080 KB |
Output is correct |