Boredom
题面翻译
题目描述
亚力克斯不喜欢无聊。
所以每当他感到无聊他就会想出一些游戏。一个冬天的晚上他想出了一个游戏并且决定开始玩这个游戏。
给定一个有 n n n 个元素的序列 { a n } \{a_n\} {an}。你可以做若干次操作。在一次操作中我们可以取出一个数(假设他为 x x x)并删除它,同时删除所有的序列中值为 x + 1 x+1 x+1 和 x − 1 x-1 x−1 的数。这一步操作会给玩家加上 x x x 分。
输入输出格式
输入格式:第一行一个整数 n ( 1 ≤ n ≤ 1 0 5 ) n(1\le n\le 10^5) n(1≤n≤105),说明这个序列有多少数。 第二行 n n n 个整数,分别表示 a 1 , a 2 , ⋯ , a n a_1,a_2,\cdots,a_n a1,a2,⋯,an。
输出格式:一个整数,表示玩家最多能获得多少分。
说明: 对于样例 3,第一步我们取 2 2 2,序列变为 [ 2 , 2 , 2 , 2 ] [2,2,2,2] [2,2,2,2]。接下来每一步都取 2 2 2,最后获得 10 10 10 分。
题目描述
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 $ a_{k} $ ) and delete it, at that all elements equal to $ a_{k}+1 $ and $ a_{k}-1 $ also must be deleted from the sequence. That step brings $ a_{k} $ 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<=10^{5} $ ) that shows how many numbers are in Alex’s sequence.
The second line contains $ n $ integers $ a_{1} $ , $ a_{2} $ , …, $ a_{n} $ ( $ 1<=a_{i}<=10^{5} $ ).
输出格式
Print a single integer — the maximum number of points that Alex can earn.
样例 #1
样例输入 #1
2
1 2
样例输出 #1
2
样例 #2
样例输入 #2
3
1 2 3
样例输出 #2
4
样例 #3
样例输入 #3
9
1 2 1 3 2 2 2 2 3
样例输出 #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.
思路:根据数据范围及题目意思我们很容易想到dp,一般dp先从一维的想起,对于dp[i]表示对于前i个分所选的最大值,因为不能相邻选,且需要计算总和因此状态表达式可以如下表示: dp[i] = max(dp[i - 1], dp[i - 2] + b[i] * i);
AC代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
typedef pair<int, double>PDD;
const int N=2e5 +10;
const int MOD = 1e9 + 7;
const int INF=0X3F3F33F;
const int dx[]={-1,0,1,0,-1,-1,+1,+1};
const int dy[]={0,1,0,-1,-1,+1,-1,+1};
//马
const int dxx[]={-1,2,1,1,-1,2,-2,-2};
const int dyy[]={2,1,-2,2,-2,-1,-1,1};
const int M = 1e7 + 10;
ll dp[N], b[N];
int main()
{
int n, res = 0;
cin >> n;
for(int i = 1; i <= n; i ++)
{
int x;
cin >> x;
b[x] ++;
res = max(x, res);
}
dp[1] = b[1];
for(int i = 1; i <= res; i ++)
{
dp[i] = max(dp[i - 1], dp[i - 2] + b[i] * i);
}
cout << dp[res] << endl;
return 0;
}