CF 455A Boredom
Posted shenben
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CF 455A Boredom相关的知识,希望对你有一定的参考价值。
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.
The first line contains integer n (1 ≤ n ≤ 105) that shows how many numbers are in Alex‘s sequence.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105).
Print a single integer — the maximum number of points that Alex can earn.
2 1 2
2
3 1 2 3
4
9 1 2 1 3 2 2 2 2 3
10
Consider the third test example. At first step we need to choose any element equal to 2. After that step our sequence looks like this [2, 2, 2, 2]. Then we do 4 steps, on each step we choose any element equals to 2. In total we earn 10 points.
【题意】
给你一个数组a,里面有n个整数。你每次可以选择数组中的一个元素ak,从数组中删掉它,再删掉所有值等于ak + 1 或者 ak - 1的元素,这样你可以得到 ak 分。你可以重复进行多次该操作,请问你最后最多能得多少分?
【分析】
先求出数列中每一个数字k的出现次数num[k]
考虑取任意一个数x时只会影响到x+1和x−1,我们可以先设dp[i]表示选取num后可以取得的最大值。因为任意取两个数a和b,若选取a后可以选取b,则选取b后可以选取a,因此我们只考虑x与x−1之间的关系。这样我们就很容易得到递推式:
注意,最后一重for循环要从2循环至已知的maxn
【代码】
#include<cstdio>
#include<iostream>
using namespace std;
const int N=1e5+5;
inline int read(){
register int x=0;register char ch=getchar();
for(;ch<‘0‘||ch>‘9‘;ch=getchar());
for(;ch>=‘0‘&&ch<=‘9‘;ch=getchar()) x=(x<<3)+(x<<1)+ch-‘0‘;
return x;
}
int n,mx;long long cnt[N],f[N];
int main(){
n=read();
for(int i=1,x;i<=n;i++) mx=max(mx,x=read()),cnt[x]+=x;
f[1]=cnt[1];
for(int i=2;i<=mx;i++) f[i]=max(f[i-1],f[i-2]+cnt[i]);
printf("%I64d
",f[mx]);
return 0;
}
以上是关于CF 455A Boredom的主要内容,如果未能解决你的问题,请参考以下文章