Submission #1030574

# Submission time Handle Problem Language Result Execution time Memory
1030574 2024-07-22T07:02:25 Z boyliguanhan Fun Tour (APIO20_fun) C++17
100 / 100
145 ms 20772 KB
#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;
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 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 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 0 ms 348 KB Output is correct
17 Correct 0 ms 348 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
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 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 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 0 ms 348 KB Output is correct
17 Correct 0 ms 348 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 0 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 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 0 ms 488 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 ms 348 KB Output is correct
40 Correct 0 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 KB Output is correct
3 Correct 1 ms 344 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 344 KB Output is correct
7 Correct 0 ms 344 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 344 KB Output is correct
12 Correct 0 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 0 ms 348 KB Output is correct
17 Correct 0 ms 348 KB Output is correct
18 Correct 67 ms 15360 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 604 KB Output is correct
21 Correct 2 ms 600 KB Output is correct
22 Correct 3 ms 1372 KB Output is correct
23 Correct 6 ms 2140 KB Output is correct
24 Correct 8 ms 3000 KB Output is correct
25 Correct 37 ms 8888 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 344 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 488 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 6 ms 1884 KB Output is correct
15 Correct 76 ms 14920 KB Output is correct
16 Correct 74 ms 15028 KB Output is correct
17 Correct 15 ms 3676 KB Output is correct
18 Correct 34 ms 7028 KB Output is correct
19 Correct 57 ms 11940 KB Output is correct
20 Correct 2 ms 856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 0 ms 344 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 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 0 ms 348 KB Output is correct
17 Correct 0 ms 348 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 0 ms 348 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 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 0 ms 488 KB Output is correct
30 Correct 0 ms 348 KB Output is correct
31 Correct 0 ms 348 KB Output is correct
32 Correct 0 ms 348 KB Output is correct
33 Correct 0 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 348 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 0 ms 348 KB Output is correct
39 Correct 0 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 0 ms 348 KB Output is correct
43 Correct 0 ms 348 KB Output is correct
44 Correct 67 ms 15360 KB Output is correct
45 Correct 1 ms 344 KB Output is correct
46 Correct 1 ms 604 KB Output is correct
47 Correct 2 ms 600 KB Output is correct
48 Correct 3 ms 1372 KB Output is correct
49 Correct 6 ms 2140 KB Output is correct
50 Correct 8 ms 3000 KB Output is correct
51 Correct 37 ms 8888 KB Output is correct
52 Correct 1 ms 348 KB Output is correct
53 Correct 6 ms 1884 KB Output is correct
54 Correct 76 ms 14920 KB Output is correct
55 Correct 74 ms 15028 KB Output is correct
56 Correct 15 ms 3676 KB Output is correct
57 Correct 34 ms 7028 KB Output is correct
58 Correct 57 ms 11940 KB Output is correct
59 Correct 2 ms 856 KB Output is correct
60 Correct 99 ms 15828 KB Output is correct
61 Correct 117 ms 18124 KB Output is correct
62 Correct 107 ms 15804 KB Output is correct
63 Correct 110 ms 19588 KB Output is correct
64 Correct 111 ms 19704 KB Output is correct
65 Correct 111 ms 15976 KB Output is correct
66 Correct 115 ms 18380 KB Output is correct
67 Correct 116 ms 19572 KB Output is correct
68 Correct 129 ms 19196 KB Output is correct
69 Correct 145 ms 20516 KB Output is correct
70 Correct 106 ms 15308 KB Output is correct
71 Correct 121 ms 19144 KB Output is correct
72 Correct 123 ms 18892 KB Output is correct
73 Correct 124 ms 18892 KB Output is correct
74 Correct 136 ms 19872 KB Output is correct
75 Correct 100 ms 16332 KB Output is correct
76 Correct 121 ms 19660 KB Output is correct
77 Correct 124 ms 19916 KB Output is correct
78 Correct 104 ms 19144 KB Output is correct
79 Correct 135 ms 20424 KB Output is correct
80 Correct 140 ms 20772 KB Output is correct
81 Correct 102 ms 15052 KB Output is correct
82 Correct 102 ms 15320 KB Output is correct
83 Correct 107 ms 15188 KB Output is correct
84 Correct 42 ms 6572 KB Output is correct