Submission #849327

# Submission time Handle Problem Language Result Execution time Memory
849327 2023-09-14T13:34:17 Z boris_mihov Longest Trip (IOI23_longesttrip) C++17
15 / 100
9 ms 856 KB
#include "longesttrip.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <random>
#include <vector>

typedef long long llong;
const int MAXN = 256 + 10;
const llong INF = 1e18;
const int INTINF = 1e9;

int n;
std::mt19937 rng(420);
std::vector <int> longest_trip(int N, int D)
{
    n = N;
    std::vector <int> a, b;
    a.push_back(0);
    for (int i = 1 ; i < n ; ++i)
    {
        bool resA = are_connected({a.back()}, {i});
        if (resA)
        {
            a.push_back(i);
        } else
        {
            if (b.empty())
            {
                b.push_back(i);
            } else
            {
                bool resB = are_connected({b.back()}, {i});
                if (resB)
                {
                    b.push_back(i);
                } else
                {
                    while (!b.empty())
                    {
                        a.push_back(b.back());
                        b.pop_back();
                    }

                    b.push_back(i);
                }
            }
        }
    }

    if (a.size() < b.size())
    {
        std::swap(a, b);
    }

    if (b.empty())
    {
        return a;
    }

    if (!are_connected(a, b))
    {
        return a;
    }

    if (are_connected({a[0]}, {b[0]}))
    {
        std::reverse(a.begin(), a.end());
        for (int i = 0 ; i < b.size() ; ++i)
        {
            a.push_back(b[i]);
        }

        return a;
    }

    if (b.size() > 1 && are_connected({a[0]}, {b.back()}))
    {
        std::reverse(b.begin(), b.end());
        std::reverse(a.begin(), a.end());
        for (int i = 0 ; i < b.size() ; ++i)
        {
            a.push_back(b[i]);
        }

        return a;
    }

    if (are_connected({b[0]}, {a.back()}))
    {
        for (int i = 0 ; i < b.size() ; ++i)
        {
            a.push_back(b[i]);
        }

        return a;
    }

    std::shuffle(a.begin(), a.end(), rng);
    std::shuffle(b.begin(), b.end(), rng);

    int aPos, bPos;
    int l = -1, r = a.size() - 1, mid;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        std::vector <int> prefix(a.begin(), a.begin() + mid);
        assert(prefix.size() == mid + 1 || b.size() == 1);
        if (!are_connected(prefix, b)) l = mid;
        else r = mid; 
    }

    aPos = r;
    std::vector <int> prefixA(a.begin(), a.begin() + aPos);
    l = -1; r = b.size() - 1;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        std::vector <int> prefixB(b.begin(), b.begin() + mid);
        if (!are_connected(prefixA, prefixB)) l = mid;
        else r = mid;
    }

    bPos = r;
    for (int i = 0 ; i < aPos ; ++i)
    {
        a.push_back(a[0]);
        a.erase(a.begin());
    }

    for (int i = 0 ; i < bPos ; ++i)
    {
        b.push_back(b[0]);
        b.erase(b.begin());
    }

    std::reverse(a.begin(), a.end());
    for (int i = 0 ; i < b.size() ; ++i)
    {
        a.push_back(b[i]);
    }

    return a;
}

Compilation message

longesttrip.cpp: In function 'std::vector<int> longest_trip(int, int)':
longesttrip.cpp:70:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   70 |         for (int i = 0 ; i < b.size() ; ++i)
      |                          ~~^~~~~~~~~~
longesttrip.cpp:82:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   82 |         for (int i = 0 ; i < b.size() ; ++i)
      |                          ~~^~~~~~~~~~
longesttrip.cpp:92:28: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   92 |         for (int i = 0 ; i < b.size() ; ++i)
      |                          ~~^~~~~~~~~~
In file included from /usr/include/c++/10/cassert:44,
                 from longesttrip.cpp:5:
longesttrip.cpp:109:30: warning: comparison of integer expressions of different signedness: 'std::vector<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
  109 |         assert(prefix.size() == mid + 1 || b.size() == 1);
      |                ~~~~~~~~~~~~~~^~~~~~~~~~
longesttrip.cpp:139:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  139 |     for (int i = 0 ; i < b.size() ; ++i)
      |                      ~~^~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 2 ms 600 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 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 596 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 344 KB Output is correct
2 Correct 5 ms 344 KB Output is correct
3 Correct 4 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 5 ms 600 KB Output is correct
6 Correct 8 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 600 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 5 ms 600 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 340 KB Output is correct
2 Correct 6 ms 344 KB Output is correct
3 Correct 4 ms 344 KB Output is correct
4 Correct 5 ms 344 KB Output is correct
5 Correct 7 ms 600 KB Output is correct
6 Correct 8 ms 344 KB Output is correct
7 Correct 6 ms 344 KB Output is correct
8 Correct 6 ms 344 KB Output is correct
9 Correct 5 ms 344 KB Output is correct
10 Correct 4 ms 344 KB Output is correct
11 Correct 5 ms 344 KB Output is correct
12 Correct 5 ms 856 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
14 Correct 7 ms 344 KB Output is correct
15 Correct 7 ms 344 KB Output is correct
16 Runtime error 2 ms 600 KB Execution killed with signal 6
17 Halted 0 ms 0 KB -
# 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 4 ms 344 KB Output is correct
4 Correct 4 ms 344 KB Output is correct
5 Correct 5 ms 344 KB Output is correct
6 Correct 7 ms 340 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 4 ms 344 KB Output is correct
12 Correct 4 ms 600 KB Output is correct
13 Correct 5 ms 344 KB Output is correct
14 Correct 9 ms 344 KB Output is correct
15 Correct 7 ms 344 KB Output is correct
16 Runtime error 1 ms 600 KB Execution killed with signal 6
17 Halted 0 ms 0 KB -