Submission #962887

# Submission time Handle Problem Language Result Execution time Memory
962887 2024-04-14T09:22:27 Z Warinchai Longest Trip (IOI23_longesttrip) C++17
70 / 100
59 ms 860 KB
#include "longesttrip.h"
#include<bits/stdc++.h>
using namespace std;
vector<int>longest_trip_3(int N){
    vector<int>ans;
    for(int i=0;i<N;i++)ans.push_back(i);
    return ans;
}
vector<int>longest_trip_2(int N){
    vector<int>ans;
    vector<int>tans;
    tans.push_back(0);
    for(int i=1;i<N;i++){
        if(are_connected({i-1},{i}))tans.push_back(i);
        else if(i!=N-1)tans.push_back(i+1),tans.push_back(i),i++;
        else ans.push_back(i);
    }
    for(auto x:tans)ans.push_back(x);
    return ans;
}
int vis[256];
int fnext(int x,int sz){
    if(sz==0)return -1;
    //cerr<<"fnext:"<<x<<" "<<sz<<"\n";
    int st=1,en=sz,ans=0;
    int id=-1;
    vector<int>ask;
    while(st<=en){
        int m=(st+en)/2;
        ask.clear();
        int cur=0;
        while(ask.size()<m){
            if(!vis[cur])ask.push_back(cur);
            cur++;
        }
        //cerr<<m<<" ask:";
        //for(auto x:ask)cerr<<x<<" ";
        //cerr<<"\n";
        if(are_connected({x},ask)){
            en=m-1;
            ans=m;
            id=cur-1;
        }else{
            st=m+1;
        }
    }
    return id;
}
pair<int,int> fconnection(vector<int>comp1,vector<int>comp2){
    int sz=comp1.size();
    //cerr<<"fnext:"<<x<<" "<<sz<<"\n";
    int st=0,en=sz-1;
    int ans1=-1,ans2=-1;
    int id1=-1;
    vector<int>ask;
    while(st<=en){
        int m=(st+en)/2;
        ask.clear();
        for(int i=0;i<=m;i++)ask.push_back(comp1[i]);
        if(are_connected(ask,comp2)){
            en=m-1;
            ans1=comp1[m];
            id1=m;
        }else{
            st=m+1;
        }
    }
    vector<int>temp;
    for(int i=0;i<=id1;i++)temp.push_back(comp1[i]);
    st=0,en=comp2.size()-1;
    while(st<=en){
        int m=(st+en)/2;
        ask.clear();
        for(int i=0;i<=m;i++)ask.push_back(comp2[i]);
        if(are_connected(ask,temp)){
            en=m-1;
            ans2=comp2[m];
        }else{
            st=m+1;
        }
    }
    return {ans1,ans2};
}
pair<int,int> fincom(int x,vector<int>ans){
    int sz=ans.size();
    //cerr<<"fnext:"<<x<<" "<<sz<<"\n";
    int st=0,en=sz-1;
    int id=-1,pos=-1;
    vector<int>ask;
    while(st<=en){
        int m=(st+en)/2;
        ask.clear();
        for(int i=0;i<=m;i++)ask.push_back(ans[i]);
        if(are_connected({x},ask)){
            en=m-1;
            id=ans[m];
            pos=m;
        }else{
            st=m+1;
        }
    }
    return {id,pos};
}
int vis2[256];
vector<int>longest_trip_1(int N){
    for(int i=0;i<256;i++)vis[i]=0,vis2[i]=0;
    vector<int>ans;
    vector<int>rans;
    vector<int>ians;
    ans.push_back(0);
    vis[0]=1;
    int cur=0,left=N-1;
    while(1){
        //cerr<<cur<<"\n";
        int x=fnext(cur,left);
        if(x!=-1){
            cur=x;
            left--;
            ans.push_back(cur);
            vis[cur]++;
        }else{
            break;
        }
    }
    for(int i=0;i<N;i++)if(!vis[i])ians.push_back(i);
    if(ans.size()==N){
        //assert(0);
        return ans;
    }
    bool check=are_connected(ans,ians);
    if(!check){
        //assert(0);
        return ans.size()>ians.size()?ans:ians;
    }
    int outcom=ians[0];
    auto x=fincom(outcom,ans);
    if(x.second==-1){
        auto x=fconnection(ans,ians);
        for(auto z:ans)if(z!=x.first)rans.push_back(z);
        rans.push_back(x.first);
        rans.push_back(x.second);
        for(auto z:ians)if(z!=x.second)rans.push_back(z);
        return rans;
    }
    for(int i=ians.size()-1;i>=0;i--)rans.push_back(ians[i]);
    assert(rans.size()==ians.size());
    //assert(x.second>=0);
    for(int i=x.second;i<ans.size();i++)rans.push_back(ans[i]);
    for(int i=0;i<x.second;i++)rans.push_back(ans[i]);
    for(int i=1;i<rans.size();i++)assert(are_connected({rans[i]},{rans[i-1]}));
    assert(ians.size()+ans.size()==N);
    //assert(rans.size()==N);
    return rans;
    /*
    vector<int>comp1,comp2;
    /*for(int i=0;i<N;i++){
        if(vis[i]||i==outcom)continue;
        if(!(are_connected({i},{outcom})))comp1.push_back(i);
        /*if(i==outcom)comp2.push_back(i);
        else if((are_connected({i},{outcom})))comp2.push_back(i);
        else comp1.push_back(i);
    }
    auto connection=fconnection(comp1,comp2);
    for(auto x:comp1)if(x!=connection.first)rans.push_back(x);
    rans.push_back(connection.first);
    rans.push_back(connection.second);
    for(auto x:comp2)if(x!=connection.second)rans.push_back(x);
    for(int i=1;i<comp1.size();i++)assert(are_connected({comp1[i]},{comp1[i-1]}));
    //for(int i=1;i<comp2.size();i++)assert(are_connected({comp2[i]},{comp2[i-1]}));
    return rans;*/
}
vector<int> longest_trip(int N, int D)
{
    if(D==1)return longest_trip_1(N);
    else if(D==2)return longest_trip_2(N);
    else return longest_trip_3(N);
    //cerr<<"end of query\n";
    return {};
}

Compilation message

longesttrip.cpp:156:5: warning: "/*" within comment [-Wcomment]
  156 |     /*for(int i=0;i<N;i++){
      |      
longesttrip.cpp:159:9: warning: "/*" within comment [-Wcomment]
  159 |         /*if(i==outcom)comp2.push_back(i);
      |          
longesttrip.cpp: In function 'int fnext(int, int)':
longesttrip.cpp:32:25: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   32 |         while(ask.size()<m){
      |               ~~~~~~~~~~^~
longesttrip.cpp:25:20: warning: variable 'ans' set but not used [-Wunused-but-set-variable]
   25 |     int st=1,en=sz,ans=0;
      |                    ^~~
longesttrip.cpp: In function 'std::vector<int> longest_trip_1(int)':
longesttrip.cpp:126:18: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  126 |     if(ans.size()==N){
      |        ~~~~~~~~~~^~~
longesttrip.cpp:148:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  148 |     for(int i=x.second;i<ans.size();i++)rans.push_back(ans[i]);
      |                        ~^~~~~~~~~~~
longesttrip.cpp:150:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  150 |     for(int i=1;i<rans.size();i++)assert(are_connected({rans[i]},{rans[i-1]}));
      |                 ~^~~~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:33,
                 from longesttrip.cpp:2:
longesttrip.cpp:151:34: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  151 |     assert(ians.size()+ans.size()==N);
      |            ~~~~~~~~~~~~~~~~~~~~~~^~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 10 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 3 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 8 ms 344 KB Output is correct
3 Correct 6 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 6 ms 344 KB Output is correct
6 Correct 5 ms 596 KB Output is correct
7 Correct 7 ms 344 KB Output is correct
8 Correct 5 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 4 ms 600 KB Output is correct
11 Correct 6 ms 600 KB Output is correct
12 Correct 5 ms 600 KB Output is correct
13 Correct 4 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 11 ms 344 KB Output is correct
3 Correct 21 ms 344 KB Output is correct
4 Correct 31 ms 600 KB Output is correct
5 Correct 46 ms 860 KB Output is correct
6 Correct 10 ms 344 KB Output is correct
7 Correct 11 ms 344 KB Output is correct
8 Correct 18 ms 344 KB Output is correct
9 Correct 28 ms 344 KB Output is correct
10 Correct 44 ms 692 KB Output is correct
11 Correct 50 ms 444 KB Output is correct
12 Correct 41 ms 452 KB Output is correct
13 Correct 45 ms 444 KB Output is correct
14 Correct 10 ms 344 KB Output is correct
15 Correct 7 ms 344 KB Output is correct
16 Correct 19 ms 344 KB Output is correct
17 Correct 23 ms 344 KB Output is correct
18 Correct 19 ms 344 KB Output is correct
19 Correct 27 ms 344 KB Output is correct
20 Correct 34 ms 344 KB Output is correct
21 Correct 39 ms 444 KB Output is correct
22 Correct 50 ms 444 KB Output is correct
23 Correct 45 ms 608 KB Output is correct
24 Correct 44 ms 448 KB Output is correct
25 Correct 9 ms 500 KB Output is correct
26 Correct 11 ms 344 KB Output is correct
27 Correct 15 ms 344 KB Output is correct
28 Correct 11 ms 344 KB Output is correct
29 Correct 16 ms 344 KB Output is correct
30 Correct 27 ms 344 KB Output is correct
31 Correct 25 ms 600 KB Output is correct
32 Correct 17 ms 344 KB Output is correct
33 Correct 27 ms 600 KB Output is correct
34 Correct 31 ms 600 KB Output is correct
35 Correct 25 ms 600 KB Output is correct
36 Correct 45 ms 448 KB Output is correct
37 Correct 49 ms 696 KB Output is correct
38 Correct 49 ms 448 KB Output is correct
39 Correct 53 ms 444 KB Output is correct
40 Correct 47 ms 448 KB Output is correct
41 Correct 42 ms 692 KB Output is correct
42 Correct 47 ms 700 KB Output is correct
43 Correct 43 ms 448 KB Output is correct
44 Correct 42 ms 700 KB Output is correct
45 Correct 13 ms 344 KB Output is correct
46 Correct 11 ms 344 KB Output is correct
47 Correct 20 ms 344 KB Output is correct
48 Correct 17 ms 344 KB Output is correct
49 Correct 18 ms 344 KB Output is correct
50 Correct 25 ms 432 KB Output is correct
51 Correct 25 ms 444 KB Output is correct
52 Correct 24 ms 600 KB Output is correct
53 Correct 35 ms 344 KB Output is correct
54 Correct 32 ms 604 KB Output is correct
55 Correct 31 ms 600 KB Output is correct
56 Correct 47 ms 604 KB Output is correct
57 Correct 51 ms 448 KB Output is correct
58 Correct 44 ms 608 KB Output is correct
59 Correct 50 ms 684 KB Output is correct
60 Correct 50 ms 448 KB Output is correct
61 Correct 47 ms 700 KB Output is correct
62 Correct 59 ms 600 KB Output is correct
63 Correct 48 ms 708 KB Output is correct
64 Correct 56 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 14 ms 344 KB Output is correct
3 Correct 22 ms 344 KB Output is correct
4 Partially correct 31 ms 436 KB Output is partially correct
5 Partially correct 44 ms 696 KB Output is partially correct
6 Correct 11 ms 344 KB Output is correct
7 Correct 14 ms 348 KB Output is correct
8 Correct 26 ms 348 KB Output is correct
9 Partially correct 31 ms 344 KB Output is partially correct
10 Partially correct 46 ms 696 KB Output is partially correct
11 Partially correct 45 ms 448 KB Output is partially correct
12 Partially correct 44 ms 444 KB Output is partially correct
13 Partially correct 43 ms 448 KB Output is partially correct
14 Correct 9 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 22 ms 344 KB Output is correct
17 Correct 23 ms 344 KB Output is correct
18 Correct 26 ms 344 KB Output is correct
19 Partially correct 34 ms 600 KB Output is partially correct
20 Partially correct 29 ms 344 KB Output is partially correct
21 Correct 8 ms 344 KB Output is correct
22 Correct 9 ms 340 KB Output is correct
23 Correct 13 ms 344 KB Output is correct
24 Correct 11 ms 344 KB Output is correct
25 Correct 12 ms 344 KB Output is correct
26 Correct 27 ms 344 KB Output is correct
27 Correct 22 ms 440 KB Output is correct
28 Correct 17 ms 344 KB Output is correct
29 Partially correct 33 ms 600 KB Output is partially correct
30 Partially correct 30 ms 348 KB Output is partially correct
31 Partially correct 27 ms 352 KB Output is partially correct
32 Correct 14 ms 344 KB Output is correct
33 Correct 11 ms 352 KB Output is correct
34 Correct 18 ms 356 KB Output is correct
35 Correct 16 ms 356 KB Output is correct
36 Correct 16 ms 356 KB Output is correct
37 Correct 26 ms 448 KB Output is correct
38 Correct 24 ms 596 KB Output is correct
39 Correct 25 ms 452 KB Output is correct
40 Partially correct 31 ms 452 KB Output is partially correct
41 Partially correct 30 ms 440 KB Output is partially correct
42 Partially correct 27 ms 420 KB Output is partially correct
43 Partially correct 43 ms 452 KB Output is partially correct
44 Partially correct 48 ms 448 KB Output is partially correct
45 Partially correct 48 ms 448 KB Output is partially correct
46 Partially correct 51 ms 444 KB Output is partially correct
47 Partially correct 44 ms 604 KB Output is partially correct
48 Partially correct 48 ms 448 KB Output is partially correct
49 Partially correct 49 ms 600 KB Output is partially correct
50 Partially correct 51 ms 612 KB Output is partially correct
51 Partially correct 41 ms 444 KB Output is partially correct
52 Partially correct 40 ms 604 KB Output is partially correct
53 Partially correct 40 ms 856 KB Output is partially correct
54 Partially correct 40 ms 608 KB Output is partially correct
55 Partially correct 42 ms 444 KB Output is partially correct
56 Partially correct 50 ms 448 KB Output is partially correct
57 Partially correct 40 ms 448 KB Output is partially correct
58 Partially correct 42 ms 684 KB Output is partially correct
59 Partially correct 41 ms 440 KB Output is partially correct
60 Partially correct 44 ms 696 KB Output is partially correct
61 Partially correct 45 ms 700 KB Output is partially correct
62 Partially correct 47 ms 672 KB Output is partially correct
63 Partially correct 45 ms 688 KB Output is partially correct
64 Partially correct 49 ms 464 KB Output is partially correct
65 Partially correct 54 ms 604 KB Output is partially correct
66 Partially correct 50 ms 448 KB Output is partially correct
67 Partially correct 46 ms 696 KB Output is partially correct
68 Partially correct 54 ms 700 KB Output is partially correct
69 Partially correct 47 ms 452 KB Output is partially correct
70 Partially correct 49 ms 436 KB Output is partially correct
71 Partially correct 50 ms 612 KB Output is partially correct
72 Partially correct 36 ms 608 KB Output is partially correct
73 Partially correct 38 ms 680 KB Output is partially correct
74 Partially correct 46 ms 456 KB Output is partially correct
75 Partially correct 52 ms 696 KB Output is partially correct
76 Partially correct 47 ms 696 KB Output is partially correct
77 Partially correct 49 ms 444 KB Output is partially correct
78 Partially correct 47 ms 608 KB Output is partially correct
79 Partially correct 47 ms 452 KB Output is partially correct
80 Partially correct 50 ms 444 KB Output is partially correct
81 Partially correct 54 ms 448 KB Output is partially correct
82 Partially correct 52 ms 700 KB Output is partially correct