Boredom
time limit per test:: 1 second | memory limit per test: 256 megabytes |
---|
description:
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.
Input
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 a1, a2, …, an (1 ≤ ai ≤ 10^5).
Output
Print a single integer — the maximum number of points that Alex can earn.
Example
Examples
Input
2
1 2
Output
2
Input
3
1 2 3
Output
4
Input
9
1 2 1 3 2 2 2 2 3
Output
10
Note:
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.
题目大意:
给出一组数字,选择一个ai,累加(并从数组中去掉这个数)。每选择一个以后,就会将数组内全部 等于ai-1 和 ai+1 的数删掉。重复选择,直到数组内没有数了,求累加和的最大值。
解题思路:
满足最优子结构的性质,采用动态规划。从小到大依次选择数,显然对于一个值n来说,如果有k个,我们肯定连续选择k次n,以获得更高的分数。
下面提供两种不同的dp方法:
思路1:
1.开设数组a[100005],a[i]表示数组中值为i的有a[i]个。f[i]表示将值为i的数全部选完以后,能达到的最大分数。
2.由于每做出一个选择,会将值为ai+1和ai-1的数全部去除,那么对于值为 i 的数来说,只有选择这个值与不选这个值两种情况。第一种情况下f[i] = f[i-2] + i*a[i]。(前i-2值能选出的最高分,不选值i-1,再选ai次值i)第二种情况下f[i] = f[i-1]。
思路2:
1.开设数组a[ ]保存数组,并对数组进行排序。f[i]表示将选到第i个数时,能达到的最大分数。
2.比较相邻两个元素的值a[i]与a[i-1]。并对不同的情况做出不同的判断:
a[i] - a[i-1]>1 , 直接选择a[i],f[i] = f[i-1]+a[i]。
a[i] - a[i-1]=1 , 找到上一个值小于a[i-1]的数的位置pos,f[i] = max( f[pos]+a[i] , f[i-1] )。一旦选择a[i],所有值为a[i-1]的数都不能选了。
a[i] = a[i-1] , 对于这种情况要注意最近的一个不等于a[i]的数是否等于a[i]-1。如果不等于,那f[i] =f[i-1]+a[i]。如果等于,那需要知道值为a[i]-1的数的最后位置pos1,以及值小于a[i]-1的数的最后位置pos2。(需要做出权衡,是否选择值a[i])f[i] = max( f[pos1] , f[pos2]+ (i-pos1)*a[i] )。
3.这种方法是面向数的,所以需要记录相等值的 数的最后位置,(至少需要记录前两个值) 这样会简化计算。如果每次去找的话,会超时!!!
注意:
1.无论是最终的累加和还是 中间的部分和 (i-pos1)*a[i] , i*a[i] 都会超过int的表示范围,需要用Long long int。
2.思路1在数字少的时候耗时是明显高于思路2的。同时如果使用思路2的方法做,一定要记录前面的想等值的最后位置,不然会超时。(test42是10^5个10^5,如果不记录的话,每次都往前找,会超时)
源代码:
思路1:
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<algorithm>
#include<vector>
using namespace std;
int n;
long long a[100005] = {0};
long long f[100005];
int main(){
scanf("%d",&n);
int num=0;
long long temp,m = 0;
for(int i=1;i<=n;i++){
scanf("%lld",&temp);
a[temp]++;
}
f[0] = 0;
f[1] = 0;
for(int i=1;i<100001&&num<n;i++){
f[i+1] = max(f[i-1]+i*a[i],f[i]);
if(a[i]!=0){
m = max(f[i+1],m);
num+=a[i];
}
}
printf("%lld",m);
return 0;
}
思路2:
#include<iostream>
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<algorithm>
#include<vector>
using namespace std;
int n;
int a[100005];
long long f[100005];
vector<int> id;
//依次保存前面的值的最后位置
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
a[0] = -1;
f[0] = 0;
int pos=-1;
for(int i=1;i<=n;i++){
if(a[i]-a[i-1]>1){
f[i] = f[i-1] + a[i];
id.push_back(i-1);
}
else if(a[i]-a[i-1]==1){
pos = id.back();
f[i] = max(f[pos]+a[i],f[i-1]);
id.push_back(i-1);
}else{
int num = i - id.back();
if(a[id.back()]==a[i]-1){
if(id.size()==1)
f[i] = max((long long)num*a[i],f[id.back()]);
else
f[i] = max((long long)num*a[i]+f[id[id.size()-2]],f[id.back()]);
}
else{
f[i] = f[i-1] + a[i];
}
}
}
printf("%lld",f[n]);
return 0;
}