結果

問題 No.919 You Are A Project Manager
ユーザー kimiyukikimiyuki
提出日時 2019-11-08 07:44:44
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 751 ms / 3,000 ms
コード長 3,657 bytes
コンパイル時間 3,435 ms
コンパイル使用メモリ 256,376 KB
実行使用メモリ 14,452 KB
最終ジャッジ日時 2024-06-27 19:42:25
合計ジャッジ時間 19,903 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 688 ms
11,364 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 64 ms
5,376 KB
testcase_05 AC 73 ms
5,376 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 198 ms
6,748 KB
testcase_08 AC 29 ms
5,376 KB
testcase_09 AC 19 ms
5,376 KB
testcase_10 AC 40 ms
5,376 KB
testcase_11 AC 38 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 53 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 13 ms
5,376 KB
testcase_16 AC 85 ms
5,376 KB
testcase_17 AC 567 ms
14,320 KB
testcase_18 AC 550 ms
11,640 KB
testcase_19 AC 682 ms
11,508 KB
testcase_20 AC 659 ms
11,120 KB
testcase_21 AC 684 ms
11,376 KB
testcase_22 AC 219 ms
6,884 KB
testcase_23 AC 212 ms
6,940 KB
testcase_24 AC 233 ms
7,136 KB
testcase_25 AC 216 ms
6,884 KB
testcase_26 AC 633 ms
10,736 KB
testcase_27 AC 536 ms
10,220 KB
testcase_28 AC 728 ms
11,256 KB
testcase_29 AC 565 ms
14,452 KB
testcase_30 AC 547 ms
11,760 KB
testcase_31 AC 553 ms
14,448 KB
testcase_32 AC 550 ms
14,324 KB
testcase_33 AC 247 ms
7,004 KB
testcase_34 AC 246 ms
7,136 KB
testcase_35 AC 748 ms
11,296 KB
testcase_36 AC 751 ms
11,504 KB
testcase_37 AC 751 ms
11,508 KB
testcase_38 AC 749 ms
11,436 KB
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 13 ms
5,376 KB
testcase_41 AC 4 ms
5,376 KB
testcase_42 AC 5 ms
5,376 KB
testcase_43 AC 7 ms
5,376 KB
testcase_44 AC 18 ms
5,376 KB
testcase_45 AC 4 ms
5,376 KB
testcase_46 AC 15 ms
5,376 KB
testcase_47 AC 217 ms
7,004 KB
testcase_48 AC 214 ms
6,752 KB
testcase_49 AC 237 ms
7,008 KB
testcase_50 AC 219 ms
6,884 KB
testcase_51 AC 193 ms
6,624 KB
testcase_52 AC 138 ms
6,360 KB
testcase_53 AC 193 ms
6,752 KB
testcase_54 AC 13 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 #

#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