結果

問題 No.31 悪のミックスジュース
ユーザー PachicobuePachicobue
提出日時 2017-07-04 01:36:11
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 44 ms / 5,000 ms
コード長 2,001 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 176,904 KB
実行使用メモリ 42,656 KB
最終ジャッジ日時 2023-10-12 17:19:46
合計ジャッジ時間 2,898 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 38 ms
37,708 KB
testcase_02 AC 38 ms
37,788 KB
testcase_03 AC 44 ms
42,640 KB
testcase_04 AC 43 ms
42,592 KB
testcase_05 AC 43 ms
42,572 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 44 ms
42,656 KB
testcase_08 AC 43 ms
42,596 KB
testcase_09 AC 43 ms
42,652 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 12 ms
13,340 KB
testcase_12 AC 31 ms
30,792 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 42 ms
41,668 KB
testcase_15 AC 8 ms
9,544 KB
testcase_16 AC 15 ms
16,824 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define show(x) cout << #x << " = " << x << endl

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v)
{
    os << "sz=" << v.size() << "\n[";
    for (const auto& p : v) {
        os << p << ",";
    }
    os << "]\n";
    return os;
}

template <typename S, typename T>
ostream& operator<<(ostream& os, const pair<S, T>& p)
{
    os << "(" << p.first << "," << p.second
       << ")";
    return os;
}


constexpr ll MOD = 1e9 + 7;

template <typename T>
constexpr T INF = numeric_limits<T>::max() / 100;

int main()
{
    int N;
    ll V;
    cin >> N >> V;
    vector<ll> cost(N);
    ll suma = 0;
    for (int i = 0; i < N; i++) {
        cin >> cost[i];
        suma += cost[i];
    }
    if (V <= N) {
        cout << suma << endl;
        return 0;
    }
    V -= N;
    vector<ll> sum(N);
    vector<pair<long double, int>> newcost(N);
    for (int i = 0; i < N; i++) {
        if (i == 0) {
            sum[i] = cost[i];
        } else {
            sum[i] = sum[i - 1] + cost[i];
        }
        newcost[i] = make_pair((long double)sum[i] / (i + 1), i);
    }
    sort(newcost.begin(), newcost.end());

    constexpr ll REST = 50000;
    const ll res = min(REST, V);
    const int ind = newcost[0].second;
    const ll vol = (V - res) / (ind + 1);
    V -= vol * (ind + 1);
    suma += vol * sum[ind];


    vector<vector<ll>> dp(N, vector<ll>(V + 1, INF<ll>));  //i番目まででLリットル作るときの最小コスト
    for (ll i = 0; i <= V; i++) {
        dp[0][i] = sum[0] * i;
    }

    for (ll i = 1; i < N; i++) {
        for (ll l = 0; l <= V; l++) {
            dp[i][l] = dp[i - 1][l];
        }
        for (ll l = 0; l <= V; l++) {
            if (l >= (i + 1)) {
                dp[i][l] = min(dp[i][l], dp[i][l - (i + 1)] + sum[i]);
            }
        }
    }
    cout << suma + dp[N - 1][V] << endl;

    return 0;
}
0