Submission #849321

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

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

int n;
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 (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;
    }

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