codeforces 455A(dp)

时间:2022-07-28
本文章向大家介绍codeforces 455A(dp),主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

题意描述

Alex doesn’t like boredom. That’s why whenever he gets bored, he comes up with games. One long winter evening he came up with a game and decided to play it.

Given a sequence a consisting of n integers. The player can make several steps. In a single step he can choose an element of the sequence (let’s denote it ak) and delete it, at that all elements equal to ak + 1 and ak - 1 also must be deleted from the sequence. That step brings ak points to the player.

Alex is a perfectionist, so he decided to get as many points as possible. Help him.

Alex每次可以选择一个数字 k k k,得到 k k k分,并且消去 k + 1 k+1 k+1和 k − 1 k-1 k−1,询问能够得到分数的最大值

思路

如果选择消除 i − 1 i-1 i−1,那么i会被消除,所以 f [ i ] = f [ i − 1 ] f[i]=f[i-1] f[i]=f[i−1],如果选择消除i,那么分数为 m a x ( f [ i − 1 ] , f [ i − 2 ] + c n t [ i ] ∗ i ) max(f[i-1],f[i-2]+cnt[i]*i) max(f[i−1],f[i−2]+cnt[i]∗i)。所以得到方程 f [ i ] = m a x ( f [ i − 1 ] , f [ i − 2 ] + c n t [ i ] ∗ i ) f[i]=max(f[i-1],f[i-2]+cnt[i]*i) f[i]=max(f[i−1],f[i−2]+cnt[i]∗i)。

AC代码

#include<bits/stdc++.h>
#define x first
#define y second
#define PB push_back
#define mst(x,a) memset(x,a,sizeof(x))
#define all(a) begin(a),end(a)
#define rep(x,l,u) for(ll x=l;x<u;x++)
#define rrep(x,l,u) for(ll x=l;x>=u;x--)
#define sz(x) x.size()
#define IOS ios::sync_with_stdio(false);cin.tie(0);
using namespace std;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef pair<long,long> PLL;
typedef pair<char,char> PCC;
typedef long long ll;
const int N=1e5+10;
const int M=1e6+10;
const int INF=0x3f3f3f3f;
const int MOD=1e9+7;
int cnt[N],a[N];
ll f[N];
void solve(){
    int n;cin>>n;
    ll minn=INF,maxn=-1;
    rep(i,1,n+1){
        ll x;cin>>x;
        cnt[x]++;
        minn=min(minn,x);
        maxn=max(maxn,x);
    }
    rep(i,minn,maxn+1){
        f[i]=max(f[i-1],f[i-2]+cnt[i]*i);
    }
    cout<<f[maxn]<<endl;
}
int main(){
    IOS;
    //int t;cin>>t;
    //while(t--){
        solve();
    //}
    return 0;
}