結果

問題 No.31 悪のミックスジュース
ユーザー tottoripapertottoripaper
提出日時 2019-01-21 20:43:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 51 ms / 5,000 ms
コード長 2,348 bytes
コンパイル時間 2,227 ms
コンパイル使用メモリ 175,388 KB
実行使用メモリ 4,356 KB
最終ジャッジ日時 2023-10-12 17:23:02
合計ジャッジ時間 3,036 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 33 ms
4,348 KB
testcase_02 AC 37 ms
4,348 KB
testcase_03 AC 51 ms
4,352 KB
testcase_04 AC 51 ms
4,356 KB
testcase_05 AC 51 ms
4,352 KB
testcase_06 AC 2 ms
4,352 KB
testcase_07 AC 51 ms
4,348 KB
testcase_08 AC 51 ms
4,352 KB
testcase_09 AC 49 ms
4,352 KB
testcase_10 AC 1 ms
4,348 KB
testcase_11 AC 3 ms
4,352 KB
testcase_12 AC 16 ms
4,352 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 47 ms
4,356 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 4 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)
#define unless(p) if(!(p))
#define until(p) while(!(p))

using ll = std::int64_t;
using P = std::tuple<int,int>;

constexpr ll INF = 1001001001001001001;

template <typename T>
struct Matrix{
    std::vector<std::vector<T>> row;
    
    Matrix() = default;
    Matrix(int n, int m) : row(n, std::vector<T>(m, INF)) {}

    std::vector<T>& operator[](int r){
        return row[r];
    }

    Matrix<T>& operator*=(Matrix<T>& rhs){
        int n = row.size(),
            m = rhs.row.size(),
            l = rhs[0].size();

        Matrix product(n, l);

        for(int i=0;i<n;++i){
            for(int j=0;j<l;++j){
                for(int k=0;k<m;++k){
                    product[i][j] = std::min(product[i][j], row[i][k] + rhs[k][j]);
                }
            }
        }

        *this = std::move(product);

        return *this;
    }

    friend Matrix<T> operator*(Matrix<T> lhs, Matrix<T>& rhs){
        lhs *= rhs;
        return lhs;
    }

    static Matrix<T> identity(int n){
        Matrix<T> I(n, n);
        for(int i=0;i<n;++i){
            I[i][i] = 0;
        }
        return I;
    }
};

template <typename T>
Matrix<T> mat_expt(Matrix<T> a, std::int64_t n){
    Matrix<T> power = std::move(Matrix<T>::identity(a.row.size()));
    
    while(n > 0){
        if(n & 1){power *= a;}
        a *= a;
        n >>= 1;
    }

    return power;
}

int N, V;
ll C[110], D[110];

int main(){
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    std::cin >> N >> V;

    for(int i=0;i<N;++i){
        std::cin >> C[i];
    }

    D[0] = C[0];
    for(int i=1;i<N;++i){
        D[i] = C[i] + D[i - 1];
    }

    if(V <= N){
        std::cout << D[N - 1] << std::endl;
        return 0;
    }

    ll minCost = D[N - 1];
    V -= N;

    Matrix<ll> A(N, N), v(N, 1);
    for(int i=0;i+1<N;++i){
        A[i][i+1] = 0;
    }

    for(int i=1;i<N;++i){
        for(int j=0;j<i;++j){
            D[i] = std::min(D[i], D[j] + D[i - j - 1]);
        }
    }

    std::copy(D, D + N, A[N-1].rbegin());

    v[0][0] = 0;
    for(int i=1;i<N;++i){
        v[i][0] = D[i - 1];
    }

    Matrix<ll> w = std::move(mat_expt<ll>(A, V) * v);

    minCost += w[0][0];
    std::cout << minCost << std::endl;
}
0