Submission #769435

#TimeUsernameProblemLanguageResultExecution timeMemory
769435boris_mihovRobots (IOI13_robots)C++17
90 / 100
3061 ms33932 KiB
#include "robots.h"
#include <algorithm>
#include <iostream>
#include <numeric>
#include <cassert>
#include <vector>
#include <bitset>
#include <set>

typedef long long llong;
const int MAXN = 1000000 + 10;
const int MAXAB = 50000 + 10;

int n;
int a, b;
int wX[MAXN];
int sX[MAXN];
int permX[MAXN];
std::multiset <int> vals;

int check(int times, int X[], int Y[])
{
    vals.clear();
    int ptr = -1;
    int cntDestroyed = 0;
    for (int curr = 0 ; curr < a ; ++curr)
    {
        while (ptr + 1 < n && wX[ptr + 1] <= X[curr])
        {
            ptr++;
            vals.insert(sX[ptr]);
        }

        for (int t = 0 ; t < times && vals.size() ; ++t)
        {
            vals.erase(std::prev(vals.end()));
        }
    }

    while (ptr + 1 < n)
    {
        ptr++;
        vals.insert(sX[ptr]);
    }

    for (int curr = 0 ; curr < b && vals.size() ; ++curr)
    {
        for (int t = 0 ; t < times && vals.size() ; ++t)
        {
            if (Y[curr] < (*vals.begin())) break;
            auto it = vals.upper_bound(Y[curr]);
            if (it == vals.begin())
            {
                break;
            } else
            {
                it = std::prev(it);
                vals.erase(it);
            }
        }
    }

    return vals.empty();
}

int putaway(int A, int B, int T, int X[], int Y[], int W[], int S[]) 
{
    a = A;
    b = B;
    n = T;

    std::iota(permX, permX + n, 0);
    std::sort(permX, permX + n, [&](const int &a, const int &b)
    {
        return W[a] < W[b];
    });

    for (int i = 0 ; i < n ; ++i)
    {
        wX[i] = W[permX[i]] + 1;
        sX[i] = S[permX[i]] + 1;
    }

    std::sort(X, X + a);
    std::sort(Y, Y + b);

    for (int i = 0 ; i < n ; ++i)
    {
        if (wX[i] > X[a - 1] && sX[i] > Y[b - 1])
        {
            return -1;
        }
    }

    // std::cout << "byW\n";
    // for (int i = 1 ; i <= n ; ++i)
    // {
    //     std::cout << w[sortedX.perm[i]] << ' ' << s[sortedX.perm[i]] << '\n';
    // }

    // std::cout << "byS\n";
    // for (int i = 1 ; i <= n ; ++i)
    // {
    //     std::cout << w[sortedY.perm[i]] << ' ' << s[sortedY.perm[i]] << '\n';
    // }

    int pw = -1;
    while (!check(1 << pw + 1, X, Y)) pw++;

    int l = (pw == -1 ? 0 : (1 << pw)), r = (1 << pw + 1), mid;
    while (l < r - 1)
    {
        mid = (l + r) / 2;
        if (!check(mid, X, Y)) l = mid;
        else r = mid;
    }

    return r;
}

Compilation message (stderr)

robots.cpp: In function 'int check(int, int*, int*)':
robots.cpp:25:9: warning: unused variable 'cntDestroyed' [-Wunused-variable]
   25 |     int cntDestroyed = 0;
      |         ^~~~~~~~~~~~
robots.cpp: In function 'int putaway(int, int, int, int*, int*, int*, int*)':
robots.cpp:108:27: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  108 |     while (!check(1 << pw + 1, X, Y)) pw++;
      |                        ~~~^~~
robots.cpp:110:54: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
  110 |     int l = (pw == -1 ? 0 : (1 << pw)), r = (1 << pw + 1), mid;
      |                                                   ~~~^~~
#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...