結果

問題 No.919 You Are A Project Manager
ユーザー kimiyuki
提出日時 2019-11-08 07:44:44
言語 C++17(1z)
(gcc 9.3.0)
結果
AC  
実行時間 733 ms / 3,000 ms
コード長 3,657 Byte
コンパイル時間 3,963 ms
使用メモリ 10,632 KB
最終ジャッジ日時 2020-05-06 05:06:06

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 675 ms
10,364 KB
testcase_01 AC 2 ms
1,572 KB
testcase_02 AC 2 ms
1,584 KB
testcase_03 AC 2 ms
1,576 KB
testcase_04 AC 62 ms
3,488 KB
testcase_05 AC 72 ms
3,492 KB
testcase_06 AC 5 ms
1,732 KB
testcase_07 AC 197 ms
5,336 KB
testcase_08 AC 30 ms
2,304 KB
testcase_09 AC 20 ms
1,896 KB
testcase_10 AC 41 ms
2,576 KB
testcase_11 AC 42 ms
2,592 KB
testcase_12 AC 3 ms
1,628 KB
testcase_13 AC 52 ms
2,572 KB
testcase_14 AC 3 ms
1,668 KB
testcase_15 AC 12 ms
1,884 KB
testcase_16 AC 83 ms
3,496 KB
testcase_17 AC 537 ms
10,632 KB
testcase_18 AC 526 ms
10,364 KB
testcase_19 AC 670 ms
10,368 KB
testcase_20 AC 648 ms
10,100 KB
testcase_21 AC 665 ms
10,368 KB
testcase_22 AC 219 ms
5,608 KB
testcase_23 AC 210 ms
5,612 KB
testcase_24 AC 230 ms
5,612 KB
testcase_25 AC 214 ms
5,608 KB
testcase_26 AC 623 ms
9,832 KB
testcase_27 AC 530 ms
9,568 KB
testcase_28 AC 706 ms
10,104 KB
testcase_29 AC 537 ms
10,632 KB
testcase_30 AC 541 ms
10,368 KB
testcase_31 AC 535 ms
10,632 KB
testcase_32 AC 527 ms
10,628 KB
testcase_33 AC 244 ms
5,608 KB
testcase_34 AC 244 ms
5,608 KB
testcase_35 AC 731 ms
10,364 KB
testcase_36 AC 732 ms
10,364 KB
testcase_37 AC 733 ms
10,364 KB
testcase_38 AC 733 ms
10,364 KB
testcase_39 AC 3 ms
1,628 KB
testcase_40 AC 13 ms
1,884 KB
testcase_41 AC 4 ms
1,688 KB
testcase_42 AC 5 ms
1,732 KB
testcase_43 AC 7 ms
1,764 KB
testcase_44 AC 19 ms
1,884 KB
testcase_45 AC 5 ms
1,728 KB
testcase_46 AC 15 ms
2,028 KB
testcase_47 AC 215 ms
5,608 KB
testcase_48 AC 213 ms
5,612 KB
testcase_49 AC 233 ms
5,612 KB
testcase_50 AC 217 ms
5,612 KB
testcase_51 AC 193 ms
5,344 KB
testcase_52 AC 134 ms
4,032 KB
testcase_53 AC 192 ms
5,344 KB
testcase_54 AC 13 ms
1,884 KB
testcase_55 AC 2 ms
1,580 KB
testcase_56 AC 2 ms
1,588 KB
testcase_57 AC 2 ms
1,596 KB
権限があれば一括ダウンロードができます

ソースコード

diff #
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/tag_and_trait.hpp>
#define REP(i, n) for (int i = 0; (i) < (int)(n); ++ (i))
#define REP3(i, m, n) for (int i = (m); (i) < (int)(n); ++ (i))
#define REP_R(i, n) for (int i = (int)(n) - 1; (i) >= 0; -- (i))
#define REP3R(i, m, n) for (int i = (int)(n) - 1; (i) >= (int)(m); -- (i))
#define ALL(x) std::begin(x), std::end(x)
using namespace std;
template <class T, class U> inline void chmax(T & a, U const & b) { a = max<T>(a, b); }
template <class Key> using rb_tree_set = __gnu_pbds::tree<Key, __gnu_pbds::null_type, std::less<Key>, __gnu_pbds::rb_tree_tag, __gnu_pbds::tree_order_statistics_node_update>;

int64_t solve(int n, const vector<int> & a) {
    // list queries for Mo's algorithm
    vector<pair<int, int> > queries;
    REP3 (k, 1, n + 1) {
        REP (i, n / k) {
            int l = k * i;
            int r = k * (i + 1);
            queries.emplace_back(l, r);
        }
        REP (i, n / k) {
            int l = n - k * (i + 1);
            int r = n - k * i;
            queries.emplace_back(l, r);
        }
    }

    // prepare the structure for Mo's algorithm
    rb_tree_set<pair<int, int> > used;
    int l = 0;
    int r = 0;
    auto add = [&](int i) {
        assert (i == l - 1 or i == r);
        used.insert(make_pair(a[i], i));
        if (i == l - 1) -- l;
        if (i == r) ++ r;
    };
    auto remove = [&](int i) {
        assert (i == l or i == r - 1);
        used.erase(make_pair(a[i], i));
        if (i == l) ++ l;
        if (i == r - 1) -- r;
    };
    auto get = [&]() {
        auto it = used.find_by_order((r - l - 1) / 2);
        assert (it != used.end());
        return it->first;
    };

    // run Mo's algorithm
    unordered_map<uint64_t, int> median;
    sort(ALL(queries));
    int sqrt_n = sqrt(n) + 3;
    for (int bl = 0, ql = 0; bl < n; ) {
        int br = min(n, bl + sqrt_n);
        int qr = ql;
        while (qr < queries.size() and queries[qr].first < br) {
            ++ qr;
        }
        sort(queries.begin() + ql, queries.begin() + qr, [&](pair<int, int> a, pair<int, int> b) {
            return a.second < b.second;
        });
        REP3 (i, ql, qr) {
            int nl, nr; tie(nl, nr) = queries[i];
            while (r < nr) add(r);
            while (nl < l) add(l - 1);
            while (nr < r) remove(r - 1);
            while (l < nl) remove(l);
            median[((uint64_t)nl << 32) | nr] = get();
        }
        bl = br;
        ql = qr;
    }

    // compute the answer
    int64_t answer = INT64_MIN;
    vector<int64_t> left, right;
    REP3 (k, 1, n + 1) {
        int cnt = n / k;

        // list left groups
        int64_t sum_left = 0;
        left.assign(cnt + 1, 0);
        REP (i, cnt) {
            int l = k * i;
            int r = k * (i + 1);
            sum_left += median.at(((uint64_t)l << 32) | r);
            left[i + 1] = max(left[i], sum_left);
        }

        // list right groups
        int64_t sum_right = 0;
        right.assign(cnt + 1, 0);
        REP (i, cnt) {
            int l = n - k * (i + 1);
            int r = n - k * i;
            sum_right += median.at(((uint64_t)l << 32) | r);
            right[i + 1] = max(right[i], sum_right);
        }

        // convolution
        REP (i, left.size()) {
            int j = right.size() - i - 1;
            chmax(answer, k * (left[i] + right[j]));
        }
    }
    return answer;
}

int main() {
    int n; cin >> n;
    vector<int> a(n);
    REP (i, n) {
        cin >> a[i];
    }
    cout << solve(n, a) << endl;
    return 0;
}
0