(補充CF 455A)退屈(DP)

原則としてリンクは〜私を突きます

効果の対象に

ゲームのスコア:文字列の数は、数取り出す\(S_I \)を、削除(a_iを+ 1 \)\\(a_iを-1 \)すべての数字の、プラススコア\(a_iを\) ポイントのほとんどの数を取得する方法

サンプル入力

2
1 2

サンプル出力

2

サンプル入力

3
1 2 3

サンプル出力

4

サンプル入力

9
1 2 1 3 2 2 2 2 3

サンプル出力

10

問題解決のためのアイデア

これは、1次元の状態DPは、Iおよび分配は、2つの状態iを取ることはない
私は、[I] = DP [I DPかかる場合 -2] + CNT [I] * I
I-1は、次に取られていない場合は不可能除去DPである[I] = DP [I -1]

サンプルコード


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

const int maxn = 1e5 + 5;
long long dp[maxn];
long long cnt[maxn];

int main()
{
    std::ios::sync_with_stdio(false);
    int n;
    cin >> n;
    int MAX=0;
    for (int i = 0; i < n; i++)
    {
        int temp;
        cin >> temp;
        MAX=max(MAX,temp);
        cnt[temp]++;
    }
    dp[1] = cnt[1];
    for(int i=2; i <= MAX; i++)
        dp[i]=max(dp[i-1],dp[i-2]+i*cnt[i]);
    cout << dp[MAX] << endl;
    return 0;
}

おすすめ

転載: www.cnblogs.com/cafu-chino/p/11862576.html