Submission #1008707

# Submission time Handle Problem Language Result Execution time Memory
1008707 2024-06-26T16:52:34 Z De3b0o Longest Trip (IOI23_longesttrip) C++17
100 / 100
14 ms 700 KB
#include "longesttrip.h"
#include<bits/stdc++.h>
#include<random>
#define ll long long
#define F first
#define S second
#define in insert
#define pb push_back
#define ppb pop_back()
#define d3 ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define cans cout << ans << "\n";
#define yes cout << "Yes" << "\n";
#define no cout << "No" << "\n";
#define pll pair<ll,ll>
#define lin cout << "\n";
#define sqr 340
#define mod 1000000007
#define mid ((l+r)/2)
#define lc (2*x)
#define rc (2*x+1)

using namespace std;

std::vector<int> longest_trip(int N, int D)
{
    ll n = N;
    vector<ll> v;
    for(int i = 0 ; n>i ; i++)
        v.pb(i);
    random_shuffle(v.begin(),v.end());
    ll l1 = v[0] , l2 = v[1];
    vector<ll> p1 , p2;
    p1.pb(v[0]);
    p2.pb(v[1]);
    bool g1 = 0;
    for(int i = 2 ; n>i ; i++)
    {
        if(are_connected({l1},{v[i]}))
        {
            p1.pb(v[i]);
            l1=v[i];
            g1 = 0;
        }
        else
        {
            if(g1)
            {
                p2.pb(v[i]);
                l2=v[i];
            }
            else
            {
                if(are_connected({l2},{v[i]}))
                {
                    p2.pb(v[i]);
                    l2=v[i];
                    g1=1;
                }
                else
                {
                    for(int j = p2.size()-1 ; j>=0 ; j--)
                        p1.pb(p2[j]);
                    l1 = p1[p1.size()-1];
                    p2.clear();
                    p2.pb(v[i]);
                    l2=v[i];
                }
            }
        }
    }
    vector<int> trnbo;
    if(p2.size()==0)
    {
        for(auto it : p1)
            trnbo.pb(it);
    }
    else
    {
        if(are_connected({p1[0]},{p2[0]}))
        {
            for(int j = p1.size()-1 ; j>=0 ; j--)
                trnbo.pb(p1[j]);
            for(auto it : p2)
                trnbo.pb(it);
        }
        else if(are_connected({p1[0]},{p2[p2.size()-1]}))
        {
            for(int j = p1.size()-1 ; j>=0 ; j--)
                trnbo.pb(p1[j]);
            for(int j = p2.size()-1 ; j>=0 ; j--)
                trnbo.pb(p2[j]);
        }
        else if(are_connected({p1[p1.size()-1]},{p2[0]}))
        {
            for(auto it : p1)
                trnbo.pb(it);
            for(auto it : p2)
                trnbo.pb(it);
        }
        else if(are_connected({p1[p1.size()-1]},{p2[p2.size()-1]}))
        {
            for(auto it : p1)
                trnbo.pb(it);
            for(int j = p2.size()-1 ; j>=0 ; j--)
                trnbo.pb(p2[j]);
        }
        else
        {
            ll l = 0 , r = p1.size()-1;
            vector<int> pp2;
            for(auto it : p2)
                pp2.pb(it);
            while(l<=r)
            {
                vector<int> v1;
                for(int j = 0 ; mid>=j ; j++)
                    v1.pb(p1[j]);
                if(are_connected(v1,pp2))
                    r=mid-1;
                else
                    l=mid+1;
            }
            r++;
            if(r==p1.size())
            {
                if(p1.size()>p2.size())
                {
                    for(auto it : p1)
                        trnbo.pb(it);
                }
                else
                {
                    for(auto it : p2)
                        trnbo.pb(it);
                }
                return trnbo;
            }
            ll f = r;
            vector<int> v1 = {p1[r]};
            l = 0;
            r = p2.size()-1;
            while(l<=r)
            {
                vector<int> v2;
                for(int j = 0 ; mid>=j ; j++)
                    v2.pb(p2[j]);
                if(are_connected(v1,v2))
                    r=mid-1;
                else
                    l=mid+1;
            }
            r++;
            ll s = r;
            for(int j = f+1 ; p1.size()>j ; j++)
                trnbo.pb(p1[j]);
            for(int j = 0 ; f>=j ; j++)
                trnbo.pb(p1[j]);
            trnbo.pb(p2[s]);
            for(int j = s+1 ; p2.size()>j ; j++)
                trnbo.pb(p2[j]);
            for(int j = 0 ; s>j ; j++)
                trnbo.pb(p2[j]);
        }
    }
    return trnbo;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:38:27: warning: narrowing conversion of 'l1' from 'long long int' to 'int' [-Wnarrowing]
   38 |         if(are_connected({l1},{v[i]}))
      |                           ^~
longesttrip.cpp:38:27: warning: narrowing conversion of 'l1' from 'long long int' to 'int' [-Wnarrowing]
longesttrip.cpp:38:37: warning: narrowing conversion of 'v.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
   38 |         if(are_connected({l1},{v[i]}))
      |                                     ^
longesttrip.cpp:38:37: warning: narrowing conversion of 'v.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:53:35: warning: narrowing conversion of 'l2' from 'long long int' to 'int' [-Wnarrowing]
   53 |                 if(are_connected({l2},{v[i]}))
      |                                   ^~
longesttrip.cpp:53:35: warning: narrowing conversion of 'l2' from 'long long int' to 'int' [-Wnarrowing]
longesttrip.cpp:53:45: warning: narrowing conversion of 'v.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
   53 |                 if(are_connected({l2},{v[i]}))
      |                                             ^
longesttrip.cpp:53:45: warning: narrowing conversion of 'v.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)i))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:79:41: warning: narrowing conversion of 'p1.std::vector<long long int>::operator[](0)' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
   79 |         if(are_connected({p1[0]},{p2[0]}))
      |                                         ^
longesttrip.cpp:79:41: warning: narrowing conversion of 'p1.std::vector<long long int>::operator[](0)' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:79:41: warning: narrowing conversion of 'p2.std::vector<long long int>::operator[](0)' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:79:41: warning: narrowing conversion of 'p2.std::vector<long long int>::operator[](0)' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:86:56: warning: narrowing conversion of 'p1.std::vector<long long int>::operator[](0)' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
   86 |         else if(are_connected({p1[0]},{p2[p2.size()-1]}))
      |                                                        ^
longesttrip.cpp:86:56: warning: narrowing conversion of 'p1.std::vector<long long int>::operator[](0)' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:86:56: warning: narrowing conversion of 'p2.std::vector<long long int>::operator[]((p2.std::vector<long long int>::size() - 1))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:86:56: warning: narrowing conversion of 'p2.std::vector<long long int>::operator[]((p2.std::vector<long long int>::size() - 1))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:93:56: warning: narrowing conversion of 'p1.std::vector<long long int>::operator[]((p1.std::vector<long long int>::size() - 1))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
   93 |         else if(are_connected({p1[p1.size()-1]},{p2[0]}))
      |                                                        ^
longesttrip.cpp:93:56: warning: narrowing conversion of 'p1.std::vector<long long int>::operator[]((p1.std::vector<long long int>::size() - 1))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:93:56: warning: narrowing conversion of 'p2.std::vector<long long int>::operator[](0)' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:93:56: warning: narrowing conversion of 'p2.std::vector<long long int>::operator[](0)' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:100:66: warning: narrowing conversion of 'p1.std::vector<long long int>::operator[]((p1.std::vector<long long int>::size() - 1))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
  100 |         else if(are_connected({p1[p1.size()-1]},{p2[p2.size()-1]}))
      |                                                                  ^
longesttrip.cpp:100:66: warning: narrowing conversion of 'p1.std::vector<long long int>::operator[]((p1.std::vector<long long int>::size() - 1))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:100:66: warning: narrowing conversion of 'p2.std::vector<long long int>::operator[]((p2.std::vector<long long int>::size() - 1))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:100:66: warning: narrowing conversion of 'p2.std::vector<long long int>::operator[]((p2.std::vector<long long int>::size() - 1))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:124:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  124 |             if(r==p1.size())
      |                ~^~~~~~~~~~~
longesttrip.cpp:139:36: warning: narrowing conversion of 'p1.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)r))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
  139 |             vector<int> v1 = {p1[r]};
      |                                    ^
longesttrip.cpp:139:36: warning: narrowing conversion of 'p1.std::vector<long long int>::operator[](((std::vector<long long int>::size_type)r))' from '__gnu_cxx::__alloc_traits<std::allocator<long long int>, long long int>::value_type' {aka 'long long int'} to 'int' [-Wnarrowing]
longesttrip.cpp:154:40: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  154 |             for(int j = f+1 ; p1.size()>j ; j++)
      |                               ~~~~~~~~~^~
longesttrip.cpp:159:40: warning: comparison of integer expressions of different signedness: 'std::vector<long long int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  159 |             for(int j = s+1 ; p2.size()>j ; j++)
      |                               ~~~~~~~~~^~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 7 ms 344 KB Output is correct
4 Correct 3 ms 344 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 10 ms 344 KB Output is correct
7 Correct 5 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 6 ms 344 KB Output is correct
10 Correct 6 ms 344 KB Output is correct
11 Correct 6 ms 344 KB Output is correct
12 Correct 6 ms 344 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 7 ms 344 KB Output is correct
3 Correct 5 ms 344 KB Output is correct
4 Correct 6 ms 340 KB Output is correct
5 Correct 4 ms 344 KB Output is correct
6 Correct 7 ms 344 KB Output is correct
7 Correct 6 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 5 ms 344 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 7 ms 344 KB Output is correct
13 Correct 7 ms 344 KB Output is correct
14 Correct 11 ms 344 KB Output is correct
15 Correct 9 ms 344 KB Output is correct
16 Correct 8 ms 344 KB Output is correct
17 Correct 5 ms 344 KB Output is correct
18 Correct 7 ms 344 KB Output is correct
19 Correct 6 ms 344 KB Output is correct
20 Correct 6 ms 344 KB Output is correct
21 Correct 6 ms 416 KB Output is correct
22 Correct 6 ms 344 KB Output is correct
23 Correct 6 ms 344 KB Output is correct
24 Correct 8 ms 344 KB Output is correct
25 Correct 12 ms 344 KB Output is correct
26 Correct 12 ms 344 KB Output is correct
27 Correct 9 ms 344 KB Output is correct
28 Correct 9 ms 344 KB Output is correct
29 Correct 9 ms 344 KB Output is correct
30 Correct 7 ms 344 KB Output is correct
31 Correct 10 ms 344 KB Output is correct
32 Correct 7 ms 344 KB Output is correct
33 Correct 5 ms 344 KB Output is correct
34 Correct 8 ms 344 KB Output is correct
35 Correct 5 ms 344 KB Output is correct
36 Correct 8 ms 344 KB Output is correct
37 Correct 6 ms 344 KB Output is correct
38 Correct 9 ms 344 KB Output is correct
39 Correct 7 ms 344 KB Output is correct
40 Correct 7 ms 424 KB Output is correct
41 Correct 7 ms 344 KB Output is correct
42 Correct 7 ms 344 KB Output is correct
43 Correct 8 ms 600 KB Output is correct
44 Correct 7 ms 344 KB Output is correct
45 Correct 7 ms 344 KB Output is correct
46 Correct 8 ms 344 KB Output is correct
47 Correct 6 ms 344 KB Output is correct
48 Correct 9 ms 344 KB Output is correct
49 Correct 6 ms 344 KB Output is correct
50 Correct 6 ms 344 KB Output is correct
51 Correct 8 ms 344 KB Output is correct
52 Correct 11 ms 344 KB Output is correct
53 Correct 8 ms 440 KB Output is correct
54 Correct 6 ms 600 KB Output is correct
55 Correct 6 ms 444 KB Output is correct
56 Correct 7 ms 600 KB Output is correct
57 Correct 6 ms 444 KB Output is correct
58 Correct 4 ms 344 KB Output is correct
59 Correct 5 ms 344 KB Output is correct
60 Correct 6 ms 600 KB Output is correct
61 Correct 6 ms 344 KB Output is correct
62 Correct 7 ms 344 KB Output is correct
63 Correct 7 ms 344 KB Output is correct
64 Correct 8 ms 444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 344 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 6 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 6 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 4 ms 344 KB Output is correct
9 Correct 7 ms 344 KB Output is correct
10 Correct 5 ms 428 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 7 ms 344 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
14 Correct 8 ms 344 KB Output is correct
15 Correct 10 ms 344 KB Output is correct
16 Correct 8 ms 344 KB Output is correct
17 Correct 5 ms 344 KB Output is correct
18 Correct 6 ms 436 KB Output is correct
19 Correct 7 ms 344 KB Output is correct
20 Correct 8 ms 344 KB Output is correct
21 Correct 14 ms 344 KB Output is correct
22 Correct 14 ms 344 KB Output is correct
23 Correct 10 ms 344 KB Output is correct
24 Correct 8 ms 344 KB Output is correct
25 Correct 8 ms 344 KB Output is correct
26 Correct 4 ms 340 KB Output is correct
27 Correct 6 ms 344 KB Output is correct
28 Correct 7 ms 344 KB Output is correct
29 Correct 6 ms 344 KB Output is correct
30 Correct 8 ms 344 KB Output is correct
31 Correct 4 ms 344 KB Output is correct
32 Correct 5 ms 344 KB Output is correct
33 Correct 8 ms 344 KB Output is correct
34 Correct 7 ms 344 KB Output is correct
35 Correct 8 ms 344 KB Output is correct
36 Correct 7 ms 344 KB Output is correct
37 Correct 5 ms 344 KB Output is correct
38 Correct 7 ms 344 KB Output is correct
39 Correct 7 ms 344 KB Output is correct
40 Correct 6 ms 344 KB Output is correct
41 Correct 5 ms 436 KB Output is correct
42 Correct 6 ms 596 KB Output is correct
43 Correct 6 ms 344 KB Output is correct
44 Correct 8 ms 428 KB Output is correct
45 Correct 6 ms 344 KB Output is correct
46 Correct 5 ms 344 KB Output is correct
47 Correct 4 ms 344 KB Output is correct
48 Correct 5 ms 432 KB Output is correct
49 Correct 4 ms 344 KB Output is correct
50 Correct 8 ms 344 KB Output is correct
51 Correct 7 ms 436 KB Output is correct
52 Correct 6 ms 344 KB Output is correct
53 Correct 6 ms 344 KB Output is correct
54 Correct 8 ms 344 KB Output is correct
55 Correct 5 ms 344 KB Output is correct
56 Correct 6 ms 344 KB Output is correct
57 Correct 8 ms 344 KB Output is correct
58 Correct 6 ms 344 KB Output is correct
59 Correct 6 ms 440 KB Output is correct
60 Correct 7 ms 432 KB Output is correct
61 Correct 6 ms 344 KB Output is correct
62 Correct 6 ms 448 KB Output is correct
63 Correct 6 ms 600 KB Output is correct
64 Correct 8 ms 344 KB Output is correct
65 Correct 8 ms 344 KB Output is correct
66 Correct 6 ms 440 KB Output is correct
67 Correct 7 ms 344 KB Output is correct
68 Correct 6 ms 344 KB Output is correct
69 Correct 8 ms 600 KB Output is correct
70 Correct 5 ms 344 KB Output is correct
71 Correct 6 ms 344 KB Output is correct
72 Correct 8 ms 444 KB Output is correct
73 Correct 7 ms 600 KB Output is correct
74 Correct 7 ms 448 KB Output is correct
75 Correct 5 ms 344 KB Output is correct
76 Correct 6 ms 344 KB Output is correct
77 Correct 5 ms 344 KB Output is correct
78 Correct 6 ms 600 KB Output is correct
79 Correct 7 ms 344 KB Output is correct
80 Correct 7 ms 448 KB Output is correct
81 Correct 8 ms 436 KB Output is correct
82 Correct 8 ms 700 KB Output is correct