結果

問題 No.919 You Are A Project Manager
ユーザー gyouzasushigyouzasushi
提出日時 2022-03-03 09:24:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 420 ms / 3,000 ms
コード長 4,807 bytes
コンパイル時間 3,238 ms
コンパイル使用メモリ 234,644 KB
実行使用メモリ 16,096 KB
最終ジャッジ日時 2023-09-24 06:10:10
合計ジャッジ時間 12,426 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 420 ms
15,544 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 44 ms
5,980 KB
testcase_05 AC 49 ms
6,360 KB
testcase_06 AC 3 ms
4,376 KB
testcase_07 AC 85 ms
8,172 KB
testcase_08 AC 16 ms
4,472 KB
testcase_09 AC 10 ms
4,376 KB
testcase_10 AC 19 ms
4,760 KB
testcase_11 AC 18 ms
4,712 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 23 ms
5,260 KB
testcase_14 AC 3 ms
4,380 KB
testcase_15 AC 7 ms
4,376 KB
testcase_16 AC 37 ms
5,792 KB
testcase_17 AC 354 ms
16,096 KB
testcase_18 AC 341 ms
15,712 KB
testcase_19 AC 413 ms
15,276 KB
testcase_20 AC 261 ms
15,068 KB
testcase_21 AC 409 ms
15,392 KB
testcase_22 AC 92 ms
8,648 KB
testcase_23 AC 87 ms
8,456 KB
testcase_24 AC 99 ms
8,548 KB
testcase_25 AC 89 ms
8,372 KB
testcase_26 AC 247 ms
14,056 KB
testcase_27 AC 200 ms
12,900 KB
testcase_28 AC 283 ms
15,060 KB
testcase_29 AC 351 ms
15,956 KB
testcase_30 AC 342 ms
15,956 KB
testcase_31 AC 355 ms
15,780 KB
testcase_32 AC 346 ms
15,800 KB
testcase_33 AC 96 ms
8,856 KB
testcase_34 AC 97 ms
8,876 KB
testcase_35 AC 288 ms
15,308 KB
testcase_36 AC 285 ms
15,212 KB
testcase_37 AC 288 ms
15,308 KB
testcase_38 AC 293 ms
15,216 KB
testcase_39 AC 2 ms
4,380 KB
testcase_40 AC 7 ms
4,376 KB
testcase_41 AC 3 ms
4,376 KB
testcase_42 AC 3 ms
4,376 KB
testcase_43 AC 5 ms
4,380 KB
testcase_44 AC 9 ms
4,376 KB
testcase_45 AC 4 ms
4,376 KB
testcase_46 AC 8 ms
4,380 KB
testcase_47 AC 91 ms
8,376 KB
testcase_48 AC 91 ms
8,336 KB
testcase_49 AC 96 ms
8,724 KB
testcase_50 AC 94 ms
8,580 KB
testcase_51 AC 86 ms
8,104 KB
testcase_52 AC 58 ms
7,096 KB
testcase_53 AC 81 ms
8,100 KB
testcase_54 AC 7 ms
4,376 KB
testcase_55 AC 1 ms
4,376 KB
testcase_56 AC 1 ms
4,376 KB
testcase_57 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(i, n) for (int i = (int)(n - 1); i >= 0; i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) int(x.size())
using namespace std;
using ll = long long;
const int INF = 1e9;
const ll LINF = 1e18;
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for (int i = 0; i < int(v.size()); i++) {
        is >> v[i];
    }
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for (int i = 0; i < int(v.size()); i++) {
        os << v[i];
        if (i < int(v.size()) - 1) os << ' ';
    }
    return os;
}

template <typename T>
struct BinaryIndexedTree {
private:
    int n;
    vector<T> data;

public:
    BinaryIndexedTree() = default;

    explicit BinaryIndexedTree(int n) : n(n) {
        data.assign(n + 1, 0);
    }

    explicit BinaryIndexedTree(const vector<T> &v)
        : BinaryIndexedTree((int)v.size()) {
        build(v);
    }

    void build(const vector<T> &v) {
        assert(n == (int)v.size());
        for (int i = 1; i <= n; i++) data[i] = v[i - 1];
        for (int i = 1; i <= n; i++) {
            int j = i + (i & -i);
            if (j <= n) data[j] += data[i];
        }
    }

    void apply(int k, const T &x) {
        for (++k; k <= n; k += k & -k) data[k] += x;
    }

    T prod(int r) const {
        T ret = T();
        for (; r > 0; r -= r & -r) ret += data[r];
        return ret;
    }

    T prod(int l, int r) const {
        return prod(r) - prod(l);
    }

    int lower_bound(T x) const {
        int i = 0;
        for (int k = 1 << (__lg(n) + 1); k > 0; k >>= 1) {
            if (i + k <= n && data[i + k] < x) {
                x -= data[i + k];
                i += k;
            }
        }
        return i;
    }

    int upper_bound(T x) const {
        int i = 0;
        for (int k = 1 << (__lg(n) + 1); k > 0; k >>= 1) {
            if (i + k <= n && data[i + k] <= x) {
                x -= data[i + k];
                i += k;
            }
        }
        return i;
    }
};
int main() {
    int n;
    cin >> n;
    vector<ll> a(n);
    cin >> a;

    auto b = a;
    sort(all(b));
    b.erase(unique(all(b)), b.end());
    rep(i, n) a[i] = lower_bound(all(b), a[i]) - b.begin();

    vector<pair<int, int>> qs;
    for (int k = 1; k <= n; k++) {
        for (int l = 0, r = k; r <= n; l += k, r += k) {
            qs.emplace_back(l, r);
        }
        for (int l = n - k, r = n; l >= 0; l -= k, r -= k) {
            qs.emplace_back(l, r);
        }
    }
    sort(all(qs));
    qs.erase(unique(all(qs)), qs.end());
    const int m = sqrt(sz(qs));
    sort(all(qs), [&](auto a, auto b) {
        auto [la, ra] = a;
        auto [lb, rb] = b;
        if (la / m != lb / m) return la / m < lb / m;
        return (la / m & 1) ? ra > rb : ra < rb;
    });

    int l = 0, r = 0;
    BinaryIndexedTree<int> bit(2 * n);
    vector<unordered_map<ll, ll>> meds(n + 1);
    for (auto [nl, nr] : qs) {
        while (l > nl) {
            --l;
            bit.apply(a[l], 1);
        }
        while (r < nr) {
            bit.apply(a[r], 1);
            r++;
        }
        while (l < nl) {
            bit.apply(a[l], -1);
            l++;
        }
        while (r > nr) {
            --r;
            bit.apply(a[r], -1);
        }
        int sum = bit.prod(2 * n);
        meds[nl][nr] = b[bit.lower_bound((sum + 1) / 2)];
    }

    vector maxs(2, vector<vector<ll>>(n + 1));
    for (int k = 1; k <= n; k++) {
        {
            ll mx = 0, sm = 0;
            maxs[0][k] = {0};
            for (int l = 0, r = k; r <= n; l += k, r += k) {
                chmax(mx, sm + ll(meds[l][r]));
                sm += meds[l][r];
                maxs[0][k].push_back(mx);
            }
        }
        {
            ll mx = 0, sm = 0;
            maxs[1][k] = {0};
            for (int l = n - k, r = n; l >= 0; l -= k, r -= k) {
                chmax(mx, sm + ll(meds[l][r]));
                sm += meds[l][r];
                maxs[1][k].push_back(mx);
            }
        }
    }

    ll ans = 0;
    for (ll k = 1; k <= n; k++) {
        int x = sz(maxs[0][k]);
        for (int i = 0; i < x; i++) {
            chmax(ans, k * (maxs[0][k][i] + maxs[1][k][x - 1 - i]));
        }
    }
    cout << ans << '\n';
}
0