結果

問題 No.919 You Are A Project Manager
ユーザー aajisakaaajisaka
提出日時 2020-04-18 01:56:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,006 ms / 3,000 ms
コード長 9,494 bytes
コンパイル時間 3,489 ms
コンパイル使用メモリ 227,100 KB
実行使用メモリ 14,460 KB
最終ジャッジ日時 2024-04-14 19:10:51
合計ジャッジ時間 22,582 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 533 ms
14,208 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 28 ms
6,940 KB
testcase_05 AC 31 ms
6,940 KB
testcase_06 AC 7 ms
6,940 KB
testcase_07 AC 347 ms
6,944 KB
testcase_08 AC 82 ms
6,944 KB
testcase_09 AC 59 ms
6,944 KB
testcase_10 AC 106 ms
6,944 KB
testcase_11 AC 100 ms
6,948 KB
testcase_12 AC 2 ms
6,940 KB
testcase_13 AC 133 ms
6,940 KB
testcase_14 AC 3 ms
6,940 KB
testcase_15 AC 36 ms
6,940 KB
testcase_16 AC 187 ms
6,944 KB
testcase_17 AC 188 ms
14,460 KB
testcase_18 AC 187 ms
14,336 KB
testcase_19 AC 529 ms
14,336 KB
testcase_20 AC 902 ms
12,528 KB
testcase_21 AC 535 ms
14,208 KB
testcase_22 AC 378 ms
6,944 KB
testcase_23 AC 374 ms
6,940 KB
testcase_24 AC 394 ms
6,940 KB
testcase_25 AC 377 ms
6,944 KB
testcase_26 AC 873 ms
12,272 KB
testcase_27 AC 758 ms
10,752 KB
testcase_28 AC 979 ms
13,824 KB
testcase_29 AC 189 ms
14,336 KB
testcase_30 AC 191 ms
14,336 KB
testcase_31 AC 184 ms
14,208 KB
testcase_32 AC 186 ms
14,208 KB
testcase_33 AC 417 ms
6,940 KB
testcase_34 AC 417 ms
6,940 KB
testcase_35 AC 1,005 ms
14,336 KB
testcase_36 AC 1,004 ms
14,336 KB
testcase_37 AC 1,002 ms
14,336 KB
testcase_38 AC 1,006 ms
14,336 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 36 ms
6,940 KB
testcase_41 AC 4 ms
6,940 KB
testcase_42 AC 7 ms
6,944 KB
testcase_43 AC 17 ms
6,944 KB
testcase_44 AC 52 ms
6,944 KB
testcase_45 AC 7 ms
6,940 KB
testcase_46 AC 41 ms
6,940 KB
testcase_47 AC 380 ms
6,940 KB
testcase_48 AC 375 ms
6,944 KB
testcase_49 AC 401 ms
6,940 KB
testcase_50 AC 376 ms
6,940 KB
testcase_51 AC 344 ms
6,944 KB
testcase_52 AC 267 ms
6,944 KB
testcase_53 AC 345 ms
6,944 KB
testcase_54 AC 36 ms
6,944 KB
testcase_55 AC 2 ms
6,940 KB
testcase_56 AC 2 ms
6,940 KB
testcase_57 AC 2 ms
6,944 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 long double PI = 3.14159265358979323846264338327950288L;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
// RBST (Randomized Binary Search Tree)
// Original: https://qiita.com/drken/items/1b7e6e459c24a83bb7fd
// BEGIN
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(SUM_UNITY), size(1), sum(SUM_UNITY) {
          left = right = nullptr;
        }

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

        /* 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(nullptr) { }
    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;
    }
    inline VAL sum() {
      return this->sum(this->root);
    }

    /* 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);
      }
    }
    void merge(RBST add) {
      this->root = this->merge(this->root, add.root);
    }
    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);
      }
    }
    RBST split(int k) {
      pair<NODE*, NODE*> sub = split(this->root, k);
      this->root = sub.first;
      return RBST(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;
    }
};
// END

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 PM {
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() {
  PM solver;
  std::istream& in(std::cin);
  std::ostream& out(std::cout);
  solver.solve(in, out);
  return 0;
}
0