結果

問題 No.777 再帰的ケーキ
ユーザー kakira9618kakira9618
提出日時 2018-12-25 02:39:52
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 792 ms / 2,000 ms
コード長 1,996 bytes
コンパイル時間 1,991 ms
コンパイル使用メモリ 179,432 KB
実行使用メモリ 35,900 KB
最終ジャッジ日時 2024-04-08 17:36:07
合計ジャッジ時間 8,323 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 1 ms
6,676 KB
testcase_03 AC 1 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 1 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 2 ms
6,676 KB
testcase_12 AC 3 ms
6,676 KB
testcase_13 AC 2 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 1 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 5 ms
6,676 KB
testcase_22 AC 6 ms
6,676 KB
testcase_23 AC 3 ms
6,676 KB
testcase_24 AC 3 ms
6,676 KB
testcase_25 AC 6 ms
6,676 KB
testcase_26 AC 6 ms
6,676 KB
testcase_27 AC 3 ms
6,676 KB
testcase_28 AC 743 ms
35,900 KB
testcase_29 AC 748 ms
35,900 KB
testcase_30 AC 792 ms
35,900 KB
testcase_31 AC 780 ms
35,900 KB
testcase_32 AC 453 ms
35,900 KB
testcase_33 AC 109 ms
6,676 KB
testcase_34 AC 166 ms
6,676 KB
testcase_35 AC 478 ms
19,532 KB
testcase_36 AC 456 ms
35,900 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define all(x) (x).begin(), (x).end()
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;

// データ構造 BIT
struct BIT {
    vector<ll> data;
    BIT(int n) : data(n + 1) {}

    // update(i, v): A[i] = max(A[i], v);
    void update(int i, ll v) {
        for(int x = i; x < data.size(); x += x & -x) {
            data[x] = max(data[x], v);
        }
    }

    // get(i): return max({A[1], A[2], ..., A[i]})
    ll get(int i) {
        ll ret = 0;
        for(int x = i; x > 0; x -= x & -x) {
            ret = max(ret, data[x]);
        }
        return ret;
    }
};

// O(N^2), TLE解
int main() {
    int N;
    cin >> N;

    map<pll, ll> Cake_; // ケーキの情報 (一時保存用)
    vector<tuple<ll,ll,ll>> Cake; // ケーキの情報 (dpで使う時用)
    for(int i = 0; i < N; i++) {
        ll a, b, c;
        cin >> a >> b >> c;

        // aの昇順、aが同じ時はbの降順にソートしつつ、同じ大きさのケーキはカロリーの高いものだけを残す
        Cake_[{a, -b}] = max(Cake_[{a, -b}], c);
    }
    N = Cake_.size(); // 省いたケーキがある場合もあるのでNを更新

    // 簡単にアクセスするために中の要素を順番を保ちつつvectorに移し替える。Bのマイナスはもとに戻す
    for(auto c : Cake_) Cake.push_back(make_tuple(c.first.first, -c.first.second, c.second));

    // 座標圧縮 (zip: B[i]をキーとして、それがB全体の小さい方から数えて何番目かを返す)
    map<ll, int> zip;
    for(int i = 0; i < N; i++) zip[get<1>(Cake[i])] = 1;
    int now = 1;
    for(auto e : zip) zip[e.first] = now++;

    // BITを使って高速化した動的計画法
    BIT bit(N);
    vector<ll> dp(N);
    for(int i = 0; i < N; i++) {
        int t = zip[get<1>(Cake[i])];
        dp[i] = get<2>(Cake[i]) + bit.get(t - 1);
        bit.update(t, dp[i]);
    }

    cout << *max_element(all(dp)) << endl;

    return 0;    
}
0