(补题 CF 455A)Boredom(DP)

原理链接戳我~

题目大意

一个得分游戏:给一串数,每次取出一个数\(s_i\),删去 \(a_i+1\)\(a_i-1\) 的所有数,总分加\(a_i\)。试问最多能得到多少分

Sample Input

2
1 2

Sample Output

2

Sample Input

3
1 2 3

Sample Output

4

Sample Input

9
1 2 1 3 2 2 2 2 3

Sample Output

10

解题思路

这是一个一维的状态dp,分取i和不取i两个状态
如果取i那么dp[i] = dp[i-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