Submission #994547


Source Code Expand

#include<bits/stdc++.h>

using namespace std;
#define int __int128

class ConvexHullDynamic
{
  typedef long long coef_t;
  typedef long long coord_t;
  typedef long long val_t;

  /*
   * Line 'y=a*x+b' represented by 2 coefficients 'a' and 'b'
   * and 'xLeft' which is intersection with previous line in hull(first line has -INF)
   */
private:
  struct Line
  {
    coef_t a, b;
    double xLeft;

    enum Type
    {
      line, maxQuery, minQuery
    } type;
    coord_t val;

    explicit Line(coef_t aa = 0, coef_t bb = 0) : a(aa), b(bb), xLeft(-INFINITY), type(Type::line), val(0) {}

    val_t valueAt(coord_t x) const { return a * x + b; }

    friend bool areParallel(const Line &l1, const Line &l2) { return l1.a == l2.a; }

    friend double intersectX(const Line &l1, const Line &l2) { return areParallel(l1, l2) ? INFINITY : 1.0 * (l2.b - l1.b) / (l1.a - l2.a); }

    bool operator<(const Line &l2) const
    {
      if(l2.type == line)
        return this->a < l2.a;
      if(l2.type == maxQuery)
        return this->xLeft < l2.val;
      if(l2.type == minQuery)
        return this->xLeft > l2.val;
    }
  };

private:
  bool isMax; //whether or not saved envelope is top(search of max value)
  std::set< Line > hull;  //envelope itself

private:
  /*
   * INFO:        Check position in hull by iterator
   * COMPLEXITY:  O(1)
   */
  bool hasPrev(std::set< Line >::iterator it) { return it != hull.begin(); }

  bool hasNext(std::set< Line >::iterator it) { return it != hull.end() && std::next(it) != hull.end(); }

  /*
   * INFO:        Check whether line l2 is irrelevant
   * NOTE:        Following positioning in hull must be true
   *              l1 is next left to l2
   *              l2 is right between l1 and l3
   *              l3 is next right to l2
   * COMPLEXITY:  O(1)
   */
  bool irrelevant(const Line &l1, const Line &l2, const Line &l3) { return intersectX(l1, l3) <= intersectX(l1, l2); }

  bool irrelevant(std::set< Line >::iterator it)
  {
    return hasPrev(it) && hasNext(it)
           && (isMax && irrelevant(*std::prev(it), *it, *std::next(it))
               || !isMax && irrelevant(*std::next(it), *it, *std::prev(it)));
  }

  /*
   * INFO:        Updates 'xValue' of line pointed by iterator 'it'
   * COMPLEXITY:  O(1)
   */
  std::set< Line >::iterator updateLeftBorder(std::set< Line >::iterator it)
  {
    if(isMax && !hasPrev(it) || !isMax && !hasNext(it))
      return it;

    double val = intersectX(*it, isMax ? *std::prev(it) : *std::next(it));
    Line buf(*it);
    it = hull.erase(it);
    buf.xLeft = val;
    it = hull.insert(it, buf);
    return it;
  }

public:
  explicit ConvexHullDynamic(bool isMax) : isMax(isMax) {}

  /*
   * INFO:        Adding line to the envelope
   *              Line is of type 'y=a*x+b' represented by 2 coefficients 'a' and 'b'
   * COMPLEXITY:  Adding N lines(N calls of function) takes O(N*log N) time
   */
  void addLine(coef_t a, coef_t b)
  {
    //find the place where line will be inserted in set
    Line l3 = Line(a, b);
    auto it = hull.lower_bound(l3);

    //if parallel line is already in set, one of them becomes irrelevant
    if(it != hull.end() && areParallel(*it, l3)) {
      if(isMax && it->b < b || !isMax && it->b > b)
        it = hull.erase(it);
      else
        return;
    }

    //try to insert
    it = hull.insert(it, l3);
    if(irrelevant(it)) {
      hull.erase(it);
      return;
    }

    //remove lines which became irrelevant after inserting line
    while(hasPrev(it) && irrelevant(std::prev(it))) hull.erase(std::prev(it));
    while(hasNext(it) && irrelevant(std::next(it))) hull.erase(std::next(it));

    //refresh 'xLine'
    it = updateLeftBorder(it);
    if(hasPrev(it))
      updateLeftBorder(std::prev(it));
    if(hasNext(it))
      updateLeftBorder(std::next(it));
  }

  /*
   * INFO:        Query, which returns max/min(depends on hull type - see more info above) value in point with abscissa 'x'
   * COMPLEXITY:  O(log N), N-amount of lines in hull
   */
  val_t getBest(coord_t x) const
  {
    Line q;
    q.val = x;
    q.type = isMax ? Line::Type::maxQuery : Line::Type::minQuery;

    auto bestLine = hull.lower_bound(q);
    if(isMax) --bestLine;
    return bestLine->valueAt(x);
  }
};


signed main()
{
  long long N, A;
  cin >> N >> A;
  if(N == 1) {
    cout << 1 << endl;
    return (0);
  }
  if(N <= A) {
    cout << N << endl;
    return (0);
  }

  ConvexHullDynamic cht(1);
  cht.addLine(1, 0);

  for(long long j = A; true; j++) {
    if(cht.getBest(j) >= N) {
      cout << j << endl;
      return (0);
    }
    long long val = cht.getBest(j - A);
    cht.addLine(val, -val * j);
  }
}

Submission Info

Submission Time
Task E - Cookies
User ei13333
Language C++14 (GCC 5.4.1)
Score 500
Code Size 4849 Byte
Status TLE
Exec Time 2107 ms
Memory 89216 KB

Judge Result

Set Name sample dataset1 dataset2
Score / Max Score 0 / 0 500 / 500 0 / 500
Status
AC × 3
AC × 27
AC × 67
TLE × 2
Set Name Test Cases
sample sample-01.txt, sample-02.txt, sample-03.txt
dataset1 sample-01.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt
dataset2 sample-01.txt, sample-02.txt, sample-03.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 01-11.txt, 01-12.txt, 01-13.txt, 01-14.txt, 01-15.txt, 01-16.txt, 01-17.txt, 01-18.txt, 01-19.txt, 01-20.txt, 01-21.txt, 01-22.txt, 01-23.txt, 01-24.txt, 01-25.txt, 01-26.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, 02-14.txt, 02-15.txt, 02-16.txt, 02-17.txt, 02-18.txt, 02-19.txt, 02-20.txt, 02-21.txt, 02-22.txt, 02-23.txt, 02-24.txt, 02-25.txt, 02-26.txt, 02-27.txt, 02-28.txt, 02-29.txt, 02-30.txt, 02-31.txt, 02-32.txt, 02-33.txt, 02-34.txt, 02-35.txt, 02-36.txt, 02-37.txt, 02-38.txt, 02-39.txt, 02-40.txt
Case Name Status Exec Time Memory
01-01.txt AC 3 ms 256 KB
01-02.txt AC 3 ms 256 KB
01-03.txt AC 3 ms 256 KB
01-04.txt AC 3 ms 256 KB
01-05.txt AC 3 ms 256 KB
01-06.txt AC 3 ms 256 KB
01-07.txt AC 3 ms 256 KB
01-08.txt AC 3 ms 256 KB
01-09.txt AC 3 ms 256 KB
01-10.txt AC 3 ms 256 KB
01-11.txt AC 3 ms 256 KB
01-12.txt AC 3 ms 256 KB
01-13.txt AC 3 ms 256 KB
01-14.txt AC 3 ms 256 KB
01-15.txt AC 3 ms 256 KB
01-16.txt AC 3 ms 256 KB
01-17.txt AC 3 ms 256 KB
01-18.txt AC 3 ms 256 KB
01-19.txt AC 3 ms 256 KB
01-20.txt AC 3 ms 256 KB
01-21.txt AC 3 ms 256 KB
01-22.txt AC 3 ms 256 KB
01-23.txt AC 3 ms 256 KB
01-24.txt AC 3 ms 256 KB
01-25.txt AC 3 ms 256 KB
01-26.txt AC 3 ms 256 KB
02-01.txt AC 3 ms 256 KB
02-02.txt AC 3 ms 256 KB
02-03.txt AC 3 ms 384 KB
02-04.txt AC 3 ms 256 KB
02-05.txt AC 3 ms 256 KB
02-06.txt AC 3 ms 256 KB
02-07.txt AC 3 ms 256 KB
02-08.txt AC 3 ms 256 KB
02-09.txt AC 3 ms 256 KB
02-10.txt AC 3 ms 256 KB
02-11.txt AC 3 ms 256 KB
02-12.txt AC 3 ms 256 KB
02-13.txt AC 3 ms 256 KB
02-14.txt AC 3 ms 256 KB
02-15.txt AC 3 ms 256 KB
02-16.txt AC 3 ms 256 KB
02-17.txt AC 3 ms 256 KB
02-18.txt AC 3 ms 384 KB
02-19.txt AC 4 ms 384 KB
02-20.txt AC 7 ms 768 KB
02-21.txt AC 40 ms 3328 KB
02-22.txt TLE 2107 ms 85248 KB
02-23.txt AC 1945 ms 78336 KB
02-24.txt AC 1926 ms 78336 KB
02-25.txt AC 3 ms 256 KB
02-26.txt AC 3 ms 256 KB
02-27.txt AC 3 ms 256 KB
02-28.txt AC 3 ms 256 KB
02-29.txt AC 3 ms 256 KB
02-30.txt AC 3 ms 256 KB
02-31.txt AC 3 ms 256 KB
02-32.txt AC 3 ms 256 KB
02-33.txt AC 3 ms 256 KB
02-34.txt AC 3 ms 256 KB
02-35.txt AC 3 ms 256 KB
02-36.txt AC 3 ms 256 KB
02-37.txt AC 4 ms 384 KB
02-38.txt AC 9 ms 1024 KB
02-39.txt AC 1696 ms 66176 KB
02-40.txt TLE 2107 ms 89216 KB
sample-01.txt AC 3 ms 256 KB
sample-02.txt AC 3 ms 256 KB
sample-03.txt AC 3 ms 256 KB