結果

問題 No.974 最後の日までに
ユーザー shibh308shibh308
提出日時 2019-12-06 23:53:29
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,872 bytes
コンパイル時間 2,570 ms
コンパイル使用メモリ 217,060 KB
実行使用メモリ 41,464 KB
最終ジャッジ日時 2023-09-07 23:23:49
合計ジャッジ時間 81,104 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 TLE -
testcase_07 TLE -
testcase_08 TLE -
testcase_09 TLE -
testcase_10 TLE -
testcase_11 TLE -
testcase_12 TLE -
testcase_13 TLE -
testcase_14 TLE -
testcase_15 TLE -
testcase_16 TLE -
testcase_17 TLE -
testcase_18 TLE -
testcase_19 TLE -
testcase_20 TLE -
testcase_21 TLE -
testcase_22 TLE -
testcase_23 TLE -
testcase_24 TLE -
testcase_25 AC 2 ms
4,376 KB
testcase_26 AC 3 ms
4,376 KB
testcase_27 AC 1,675 ms
33,152 KB
testcase_28 TLE -
testcase_29 AC 2 ms
4,380 KB
testcase_30 TLE -
testcase_31 AC 1 ms
4,376 KB
testcase_32 AC 2 ms
4,380 KB
testcase_33 AC 1,124 ms
22,960 KB
testcase_34 TLE -
testcase_35 AC 1 ms
4,380 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 TLE -
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 1,522 ms
25,432 KB
testcase_41 TLE -
testcase_42 TLE -
testcase_43 TLE -
testcase_44 AC 1,571 ms
28,628 KB
testcase_45 AC 1,059 ms
20,840 KB
testcase_46 AC 1 ms
4,376 KB
testcase_47 AC 2 ms
4,380 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 2 ms
4,376 KB
testcase_51 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;

using i64 = long long;


signed main(){
    int n;
    cin >> n;
    vector<i64> a(n), b(n), c(n);
    for(int i = 0; i < n; ++i)
        cin >> a[i] >> b[i] >> c[i];

    array<int, 2> m{n / 2, (n + 1) / 2};


    // v[k][flag] = (money, val)
    vector<vector<pair<i64,i64>>> v(2);
    vector<vector<pair<i64,i64>>> special(2);

    for(int k = 0; k < 2; ++k){
        for(int i = 0; i < (1 << m[k]); ++i){
            i64 money = 0;
            i64 val = 0;
            bool flag = false;
            bool ignore = false;
            for(int j = 0; j < m[k]; ++j){
                if((i >> j) & 1){
                    if(flag){
                        ignore = true;
                        break;
                    }
                    flag = true;
                }
                else{
                    if(flag){
                        val += b[j + m[0] * k];
                        money -= c[j + m[0] * k];
                    }
                    else{
                        money += a[j + m[0] * k];
                    }
                    flag = false;
                }
            }
            if(!ignore){
                v[k].emplace_back(money, val);
                if(!k && flag)
                    special[k].emplace_back(money, val);
                if(k && !(i & 1))
                    special[k].emplace_back(money - a[m[0]] - c[m[0]], val + b[m[0]]);
            }
        }
    }

    vector<vector<i64>> v_keys(2), special_keys(2);
    vector<vector<i64>> v_vals(2), special_vals(2);
    for(int i = 0; i < 2; ++i){
        for(auto& p : v[i])
            v_keys[i].emplace_back(p.first);
        for(auto& p : special[i])
            special_keys[i].emplace_back(p.first);
        sort(v_keys[i].begin(), v_keys[i].end());
        sort(special_keys[i].begin(), special_keys[i].end());
        v_keys[i].erase(unique(v_keys[i].begin(), v_keys[i].end()), v_keys[i].end());
        special_keys[i].erase(unique(special_keys[i].begin(), special_keys[i].end()), special_keys[i].end());

        v_vals[i].assign(v_keys[i].size(), -1e18);
        special_vals[i].assign(special_keys[i].size(), -1e18);

        for(auto& p : v[i]){
            int idx = distance(v_keys[i].begin(), lower_bound(v_keys[i].begin(), v_keys[i].end(), p.first));
            v_vals[i][idx] = max(v_vals[i][idx], p.second);
        }
        for(auto& p : special[i]){
            int idx = distance(special_keys[i].begin(), lower_bound(special_keys[i].begin(), special_keys[i].end(), p.first));
            special_vals[i][idx] = max(special_vals[i][idx], p.second);
        }
        for(int j = v_keys[i].size() - 1; j > 0; --j)
            v_vals[i][j - 1] = max(v_vals[i][j - 1], v_vals[i][j]);
        for(int j = special_vals[i].size() - 1; j > 0; --j)
            special_vals[i][j - 1] = max(special_vals[i][j - 1], special_vals[i][j]);
    }

    i64 ans = 0;
    for(int i = 0; i < v_keys[0].size(); ++i){
        auto it = lower_bound(v_keys[1].begin(), v_keys[1].end(), -v_keys[0][i]);
        if(it == v_keys[1].end()){
            continue;
        }
        int idx = distance(v_keys[1].begin(), it);
        ans = max(ans, v_vals[0][i] + v_vals[1][idx]);
    }

    for(int i = 0; i < special_keys[0].size(); ++i){
        auto it = lower_bound(special_keys[1].begin(), special_keys[1].end(), -special_keys[0][i]);
        if(it == special_keys[1].end()){
            continue;
        }
        int idx = distance(special_keys[1].begin(), it);
        ans = max(ans, special_vals[0][i] + special_vals[1][idx]);
    }

    /*
    for(int i = 0; i < 2; ++i){
        for(auto &x : v_keys[i])
            cout << x << " ";
        cout << endl;
        for(auto &x : v_vals[i])
            cout << x << " ";
        cout << endl;
        cout << endl;
    }
     */

    cout << ans << endl;
}
0