結果

問題 No.919 You Are A Project Manager
ユーザー aajisakaaajisaka
提出日時 2019-09-22 17:23:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 978 ms / 3,000 ms
コード長 9,041 bytes
コンパイル時間 3,006 ms
コンパイル使用メモリ 199,172 KB
実行使用メモリ 14,336 KB
最終ジャッジ日時 2024-06-27 16:48:32
合計ジャッジ時間 21,335 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 519 ms
14,208 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 29 ms
5,376 KB
testcase_05 AC 32 ms
5,376 KB
testcase_06 AC 8 ms
5,376 KB
testcase_07 AC 334 ms
5,632 KB
testcase_08 AC 80 ms
5,376 KB
testcase_09 AC 56 ms
5,376 KB
testcase_10 AC 103 ms
5,376 KB
testcase_11 AC 97 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 128 ms
5,376 KB
testcase_14 AC 4 ms
5,376 KB
testcase_15 AC 36 ms
5,376 KB
testcase_16 AC 181 ms
5,376 KB
testcase_17 AC 186 ms
14,208 KB
testcase_18 AC 188 ms
14,208 KB
testcase_19 AC 514 ms
14,080 KB
testcase_20 AC 869 ms
12,404 KB
testcase_21 AC 519 ms
14,080 KB
testcase_22 AC 368 ms
5,888 KB
testcase_23 AC 351 ms
5,760 KB
testcase_24 AC 380 ms
6,144 KB
testcase_25 AC 369 ms
5,888 KB
testcase_26 AC 837 ms
12,152 KB
testcase_27 AC 732 ms
10,624 KB
testcase_28 AC 948 ms
13,824 KB
testcase_29 AC 188 ms
14,336 KB
testcase_30 AC 192 ms
14,208 KB
testcase_31 AC 184 ms
14,080 KB
testcase_32 AC 183 ms
14,208 KB
testcase_33 AC 403 ms
6,272 KB
testcase_34 AC 406 ms
6,144 KB
testcase_35 AC 970 ms
14,080 KB
testcase_36 AC 975 ms
14,080 KB
testcase_37 AC 971 ms
14,080 KB
testcase_38 AC 978 ms
14,208 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 35 ms
5,376 KB
testcase_41 AC 4 ms
5,376 KB
testcase_42 AC 7 ms
5,376 KB
testcase_43 AC 17 ms
5,376 KB
testcase_44 AC 51 ms
5,376 KB
testcase_45 AC 7 ms
5,376 KB
testcase_46 AC 39 ms
5,376 KB
testcase_47 AC 367 ms
6,016 KB
testcase_48 AC 362 ms
6,016 KB
testcase_49 AC 389 ms
6,272 KB
testcase_50 AC 366 ms
6,016 KB
testcase_51 AC 333 ms
5,504 KB
testcase_52 AC 259 ms
5,376 KB
testcase_53 AC 330 ms
5,504 KB
testcase_54 AC 35 ms
5,376 KB
testcase_55 AC 2 ms
5,376 KB
testcase_56 AC 2 ms
5,376 KB
testcase_57 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author aajisaka
 */

#include<bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
  cerr << " " << to_string(H);
  debug_out(T...);
}
#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SPEED ios_base::sync_with_stdio(false);cin.tie(nullptr)
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define all(v) v.begin(), v.end()
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

using ll = long long;
using P = pair<ll, ll>;

constexpr double PI = 3.14159265358979323846;
mt19937_64 engine(chrono::steady_clock::now().time_since_epoch().count());

template<class VAL> struct RBST {
    VAL SUM_UNITY = 0;                              // to be set

    unsigned int randInt() {
      static unsigned int tx = 123456789, ty = 362436069, tz = 521288629, tw = 88675123;
      unsigned int tt = (tx ^ (tx << 11));
      tx = ty; ty = tz; tz = tw;
      return (tw = (tw ^ (tw >> 19)) ^ (tt ^ (tt >> 8)));
    }

    struct NODE {
        NODE *left, *right;
        VAL val;                        // the value of the node
        int size;                       // the size of the subtree
        VAL sum;                        // the value-sum of the subtree

        NODE() : val(0), size(1), sum(0) {
          left = right = NULL;
        }

        NODE(VAL v) : val(v), size(1), sum(v) {
          left = right = NULL;
        }

        /* additional update */
        inline void update() {

        }

        /* additional lazy-propagation */
        inline void push() {

          /* ex: reverse */
          /*
          if (this->rev) {
          swap(this->left, this->right);
          if (this->left) this->left->rev ^= true;
          if (this->right) this->right->rev ^= true;
          this->rev = false;
          }
          */
        }
    };


    ///////////////////////
    // root
    ///////////////////////

    NODE* root;
    RBST() : root(NULL) { }
    RBST(NODE* node) : root(node) { }


    ///////////////////////
    // basic operations
    ///////////////////////

    /* size */
    inline int size(NODE *node) {
      return !node ? 0 : node->size;
    }
    inline int size() {
      return this->size(this->root);
    }

    /* sum */
    inline VAL sum(NODE *node) {
      return !node ? SUM_UNITY : node->sum;
    }

    /* update, push */
    inline NODE* update(NODE *node) {
      node->size = size(node->left) + size(node->right) + 1;
      node->sum = sum(node->left) + sum(node->right) + node->val;
      node->update();
      return node;
    }

    inline void push(NODE *node) {
      if (!node) return;
      node->push();
    }

    /* lower_bound */
    inline int lowerBound(NODE *node, VAL val) {
      push(node);
      if (!node) return 0;
      if (val <= node->val) return lowerBound(node->left, val);
      else return size(node->left) + lowerBound(node->right, val) + 1;
    }
    inline int lowerBound(VAL val) {
      return this->lowerBound(this->root, val);
    }

    /* upper_bound */
    inline int upperBound(NODE *node, VAL val) {
      push(node);
      if (!node) return 0;
      if (val >= node->val) return size(node->left) + upperBound(node->right, val) + 1;
      else return upperBound(node->left, val);
    }
    inline int upperBound(VAL val) {
      return this->upperBound(this->root, val);
    }

    /* count */
    inline int count(VAL val) {
      return upperBound(val) - lowerBound(val);
    }

    /* get --- k: 0-index */
    inline VAL get(NODE *node, int k) {
      push(node);
      if (!node) return -1;
      if (k == size(node->left)) return node->val;
      if (k < size(node->left)) return get(node->left, k);
      else return get(node->right, k - size(node->left) - 1);
    }
    inline VAL get(int k) {
      return get(this->root, k);
    }


    ///////////////////////
    // merge-split
    ///////////////////////

    NODE* merge(NODE *left, NODE *right) {
      push(left);
      push(right);
      if (!left || !right) {
        if (left) return left;
        else return right;
      }
      if (randInt() % (left->size + right->size) < left->size) {
        left->right = merge(left->right, right);
        return update(left);
      }
      else {
        right->left = merge(left, right->left);
        return update(right);
      }
    }
    pair<NODE*, NODE*> split(NODE* node, int k) { // [0, k), [k, n)
      push(node);
      if (!node) return make_pair(node, node);
      if (k <= size(node->left)) {
        pair<NODE*, NODE*> sub = split(node->left, k);
        node->left = sub.second;
        return make_pair(sub.first, update(node));
      }
      else {
        pair<NODE*, NODE*> sub = split(node->right, k - size(node->left) - 1);
        node->right = sub.first;
        return make_pair(update(node), sub.second);
      }
    }

    ///////////////////////
    // insert-erase
    ///////////////////////

    void insert(const VAL val) {
      pair<NODE*, NODE*> sub = this->split(this->root, this->lowerBound(val));
      this->root = this->merge(this->merge(sub.first, new NODE(val)), sub.second);
    }

    void erase(const VAL val) {
      if (!this->count(val)) return;
      pair<NODE*, NODE*> sub = this->split(this->root, this->lowerBound(val));
      this->root = this->merge(sub.first, this->split(sub.second, 1).second);
    }


    ///////////////////////
    // debug
    ///////////////////////

    void print(NODE *node) {
      if (!node) return;
      push(node);
      print(node->left);
      cout << node->val << " ";
      print(node->right);
    }
    void print() {
      cout << "{";
      print(this->root);
      cout << "}" << endl;
    }
};

class Median {
    priority_queue<ll> low;
    priority_queue<ll, vector<ll>, greater<>> high;
    int count;

public: void push(ll a) {
      if (count % 2 == 0) {
        low.push(a);
      } else {
        high.push(a);
      }
      if (low.top() > high.top()) {
        auto left = low.top();
        low.pop();
        auto right = high.top();
        high.pop();
        high.push(left);
        low.push(right);
      }
      count++;
    }

    Median() {
      low.push(LLONG_MIN);
      high.push(LLONG_MAX);
      count = 0;
    }

public: ll get() {
      return low.top();
    }
};

const ll h = pow(10000, (double)2/3);

class CLCMs {
public:
    void solve(istream& cin, ostream& cout) {
      SPEED;
      ll n; cin >> n;
      assert(n>=1);
      assert(n<=10000);
      vector<ll> a(n);
      rep(i, n) {
        cin >> a[i];
        assert(-1000000000 <= a[i]);
        assert(a[i] <= 1000000000);
      }
      auto ra = a;
      reverse(all(ra));

      ll ans = 0;
      for(int k=1; k<=min(h,n); k++) {
        vector<ll> v1, v2;
        v1.push_back(0); v2.push_back(0);
        ll no1 = 0; ll no2 = 0;
        ll ma1 = 0; ll ma2 = 0;
        for(int i=0; i+k<=n; i+=k) {
          auto med1 = Median();
          auto med2 = Median();
          for(int j=i; j<i+k; j++) {
            med1.push(a[j]);
            med2.push(ra[j]);
          }
          no1 += med1.get();
          chmax(ma1, no1);
          v1.push_back(ma1);

          no2 += med2.get();
          chmax(ma2, no2);
          v2.push_back(ma2);
        }
        reverse(all(v2));
        int r = v1.size();
        rep(i, r) {
          chmax(ans, (v1[i]+v2[i])*k);
        }
      }

      auto q = n/(h+1);
      vector<RBST<ll>> vm1(q), vm2(q);
      rep(i, q*(h+1)) {
        vm1[i/(h+1)].insert(a[i]);
        vm2[i/(h+1)].insert(ra[i]);
      }
      for(int k=h+1; k<=n; k++) {
        int t = vm1.size();
        vector<ll> v1(1), v2(1);
        ll no1 = 0; ll no2 = 0;
        ll ma1 = 0; ll ma2 = 0;
        rep(i, t) {
          no1 += vm1[i].get((k-1)/2);
          chmax(ma1, no1);
          v1.push_back(ma1);
          no2 += vm2[i].get((k-1)/2);
          chmax(ma2, no2);
          v2.push_back(ma2);
        }
        reverse(all(v2));
        rep(i, t+1) {
          chmax(ans, (v1[i]+v2[i])*k);
        }

        rep(i, t) {
          int bleft = i*k;
          int bright = (i+1)*k;
          int aleft = i*(k+1);
          int aright = (i+1)*(k+1);
          if (aright > n) {
            vm1.pop_back();
            vm2.pop_back();
            continue;
          }
          for(int j=bleft; j<aleft; j++) {
            vm1[i].erase(a[j]);
            vm2[i].erase(ra[j]);
          }
          for(int j=bright; j<aright; j++) {
            vm1[i].insert(a[j]);
            vm2[i].insert(ra[j]);
          }
        }
      }
      cout << ans << endl;
    }
};

signed main() {
  CLCMs solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
0