Submission #845652

#TimeUsernameProblemLanguageResultExecution timeMemory
845652ogkostyaOvertaking (IOI23_overtaking)C++17
100 / 100
480 ms46960 KiB
#include "overtaking.h"
#include <algorithm>
#include <utility>
#include <list>
#include <climits>

class Point
{
public:
    long long t, w;
    int i;
};

struct sort1
{
    inline bool operator() (Point const& a, Point const& b)
    {
        if (a.t == b.t)
            return a.w < b.w;
        return a.t < b.t;
    }
};

struct sort2
{
    inline bool operator() (Point const& a, Point const& b)
    {
        if (a.t == b.t)
            return b.w < a.w;
        return a.t < b.t;
    }
};

int _M;
std::vector<int> _S;
int _X;

std::vector<std::vector<long long>> arr;
std::vector<std::vector<long long>> ans;

void init(int L, int N, std::vector<long long> T, std::vector<int> W, int X, int M, std::vector<int> S)
{
    _X = X;
    _M = M;
    _S = S;

    std::vector<Point> TW;
    TW.reserve(N);
    for (int i = 0; i < N; i++)
    {
        if (W[i] < X)
            continue;
        Point p = { T[i], W[i] };
        TW.push_back(p);
    }
    if (TW.size() == 0)
        return;
    long long mb = 0;
    arr = std::vector<std::vector<long long>>(TW.size(), std::vector<long long>(M));
    ans = std::vector<std::vector<long long>>(TW.size(), std::vector<long long>(M));
    std::sort(TW.begin(), TW.end(), sort1());
    for (int i = 0; i < TW.size(); i++)
    {
        arr[i][0] = TW[i].t;
        TW[i].i = i;
    }
    for (int j = 1; j < M; j++)
    {
        std::sort(TW.begin(), TW.end(), sort1());
        mb = 0;
        for (int i = 0; i < TW.size(); i++)
        {
            long long a = TW[i].t;
            long long b = a + (_S[j] - _S[j - 1]) * TW[i].w;

            if (b < mb)
                b = mb;
            else
                mb = b;

            arr[i][j] = b;
            TW[i].t = b;
        }
    }
    for (int i = 0; i < TW.size(); i++)
    {
        ans[i][M - 1] = arr[i][M - 1];
    }
    for (int j = M - 2; j >= 1; j--)
    {
        ans[0][j] = arr[0][j] + 1LL * (_S[M - 1] - _S[j]) * X;

        for (int i = 1; i < TW.size(); i++)
        {
            if (arr[i][j] == arr[i - 1][j])
            {
                ans[i][j] = ans[i - 1][j];
            }
            else
            {
                int ii = i - 1;
                int l = j;
                int r = arr[ii].size();
                while (l < r)
                {
                    int m = (l + r) / 2;
                    long long c = arr[i][j] + 1LL * (_S[m] - _S[j]) * X;
                    if (arr[ii][m] < c)
                    {
                        l = m + 1;
                    }
                    else
                    {
                        r = m;
                    }
                }
                if (l < arr[ii].size())
                {
                    ans[i][j] = ans[ii][l];
                }
                else
                {
                    ans[i][j] = arr[i][j] + 1LL * (_S[M-1] - _S[j]) * X;
                }
            }
        }
    }
}

long long arrival_time(long long Y)
{
    long long b = Y + 1LL * _S[_M - 1] * _X;
    if (arr.size() == 0)
        return b;

    int i = 0;

    if (arr[0][0] >= Y)
        return b;

    int l = 0, r = arr.size();
    while (l < r)
    {
        int m = (l + r + 1) / 2;
        if (m == arr.size())
        {
            break;
        }
        else if (arr[m][0] < Y)
        {
            l = m;
        }
        else
        {
            r = m - 1;
        }
    }
    i = l;
    l = 0;
    r = arr[i].size();
    while (l < r)
    {
        int m = (l + r) / 2;
        long long c = Y + 1LL * _S[m] * _X;
        if (arr[i][m] < c)
        {
            l = m + 1;
        }
        else
        {
            r = m;
        }
    }
    if (l == arr[i].size())
        return b;

    return ans[i][l];
}

Compilation message (stderr)

overtaking.cpp: In function 'void init(int, int, std::vector<long long int>, std::vector<int>, int, int, std::vector<int>)':
overtaking.cpp:62:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   62 |     for (int i = 0; i < TW.size(); i++)
      |                     ~~^~~~~~~~~~~
overtaking.cpp:71:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   71 |         for (int i = 0; i < TW.size(); i++)
      |                         ~~^~~~~~~~~~~
overtaking.cpp:85:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   85 |     for (int i = 0; i < TW.size(); i++)
      |                     ~~^~~~~~~~~~~
overtaking.cpp:93:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   93 |         for (int i = 1; i < TW.size(); i++)
      |                         ~~^~~~~~~~~~~
overtaking.cpp:117:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  117 |                 if (l < arr[ii].size())
      |                     ~~^~~~~~~~~~~~~~~~
overtaking.cpp: In function 'long long int arrival_time(long long int)':
overtaking.cpp:145:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  145 |         if (m == arr.size())
      |             ~~^~~~~~~~~~~~~
overtaking.cpp:174:11: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  174 |     if (l == arr[i].size())
      |         ~~^~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...