結果

問題 No.324 落ちてた閉路グラフ
ユーザー tubo28tubo28
提出日時 2015-12-15 13:00:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 77 ms / 5,000 ms
コード長 2,762 bytes
コンパイル時間 1,463 ms
コンパイル使用メモリ 147,816 KB
実行使用メモリ 74,124 KB
最終ジャッジ日時 2023-08-09 21:24:46
合計ジャッジ時間 4,135 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 68 ms
73,992 KB
testcase_05 AC 77 ms
73,956 KB
testcase_06 AC 64 ms
73,976 KB
testcase_07 AC 74 ms
73,944 KB
testcase_08 AC 66 ms
73,892 KB
testcase_09 AC 62 ms
74,004 KB
testcase_10 AC 65 ms
74,124 KB
testcase_11 AC 61 ms
73,972 KB
testcase_12 AC 54 ms
68,952 KB
testcase_13 AC 35 ms
44,320 KB
testcase_14 AC 19 ms
23,936 KB
testcase_15 AC 25 ms
29,924 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 1 ms
4,380 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
testcase_30 AC 2 ms
4,376 KB
testcase_31 AC 2 ms
4,380 KB
testcase_32 AC 74 ms
73,896 KB
testcase_33 AC 72 ms
73,936 KB
testcase_34 AC 55 ms
61,656 KB
testcase_35 AC 7 ms
7,072 KB
testcase_36 AC 42 ms
51,020 KB
testcase_37 AC 6 ms
7,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

const int inf = 1e9;

const int MIN_N = 3;
const int MAX_N = 3000;
const int MIN_M = 0;
const int MAX_M = MAX_N;
const int MIN_W = -300;
const int MAX_W = 300;

int naive(const vector<int> &w, int m){
    int n = w.size();
    int best = -inf;
    for(int S = 0; S < 1<<n; S++){
        int c = __builtin_popcount(S);
        if(c != m) continue;
        int sum = 0;
        for(int i = 0; i < n; i++){
            if((S >> i & 1) & (S >> ((i+1)%n) & 1)){
                sum += w[i];
            }
        }
        best = max(best, sum);
    }
    return best;
}

int solve(vector<int> w, int m){
    int n = w.size(), ans = -inf;
    for(int t = 0; t < 2; t++){ // t = 0 -> 0 番目を取る, t = 1 -> とらない
        static int dp[MAX_N+10][MAX_M+10][2]; // [0,i) 個めで j 個とったときの最大値
        for(int i = 0; i <= n; i++){
            for(int j = 0; j <= m; j++){
                dp[i][j][0] = dp[i][j][1] = -inf;
            }
        }
        dp[0][t][t] = 0;
        for(int i = 0; i < n; ++i) {
            for(int j = 0; j <= MAX_M; ++j) {
                // w[i] をとる
                dp[i+1][j+1][1] = max(dp[i+1][j+1][1], dp[i][j][0]); // 前のをとっていない
                dp[i+1][j+1][1] = max(dp[i+1][j+1][1], dp[i][j][1] + w[i]); // 前のをとっている
                // w[i] をとらない
                dp[i+1][j][0]   = max(dp[i+1][j][0], dp[i][j][0]); // 前のをとっていない
                dp[i+1][j][0]   = max(dp[i+1][j][0], dp[i][j][1]); // 前のをとっている
            }
            if(t == 1){
                if(i == n-1) ans = max(ans, dp[i][m][t]+w[n-1]);
                else if(i != n) ans = max(ans, dp[i][m][t]);
            } else {
                ans = max(ans, dp[i][m][t]);
            }
        }
        if(t == 0) ans = max(ans, dp[n][m][0]);
    }
    return ans;
}

inline int rand(int l, int r){
    int w = r - l + 1;
    return rand() % w + l;
}

#define rep(i,n) for(int i = 0; i < (int)(n); i++)

int main(){
    int n,m;
    while(cin >> n >> m){
        vector<int> w(n);
        rep(i,n) cin >> w[i];
        assert(MIN_N <= n && n <= MAX_N);
        assert(MIN_M <= m && m <= MAX_M);
        rep(i,n) assert(MIN_W <= w[i] && w[i] <= MAX_W);
        int res = solve(w,m);
        cout << res << endl;
        // int exp = naive(w,m);
        // if(exp != res){
        //     cout << n << ' ' << m << endl;
        //     for(int i = 0; i < n; i++){
        //         cout << w[i] << ' ';
        //     }
        //     cout << endl;
        //     cout << "res = " << res << endl;
        //     cout << "exp = " << exp << endl;
        //     abort();
        // }
        // return 0;
    }
}
0