Codeforces Round #260 (Div. 1) A - Boredom DP
A. Boredom
Time Limit: 20 Sec
Memory Limit: 256 MB
题目连接
http://codeforces.com/contest/455/problem/A
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.
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 ≤ 105) that shows how many numbers are in Alex's sequence.
The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 105).
Output
Print a single integer — the maximum number of points that Alex can earn.
Sample Input
2
1 2
Sample Output
2
HINT
题意
给你n个数,你每次可以选择删除去一个数x,但是等于x+1和等于x-1的数都得删去
你每一次操作可以得x分
题解:
dp,dp[i]表示到i后能够得到的最大分数
dp[i]=max(dp[i-1],dp[i-2]+a[i-1]*(i-1));
代码:
#include <cstdio> #include <cmath> #include <cstring> #include <ctime> #include <iostream> #include <algorithm> #include <set> #include <vector> #include <sstream> #include <queue> #include <typeinfo> #include <fstream> #include <map> #include <stack> typedef long long ll; using namespace std; //freopen("D.in","r",stdin); //freopen("D.out","w",stdout); #define sspeed ios_base::sync_with_stdio(0);cin.tie(0) #define test freopen("test.txt","r",stdin) #define maxn 2000001 #define mod 10007 #define eps 1e-5 const int inf=0x3f3f3f3f; const ll infll = 0x3f3f3f3f3f3f3f3fLL; inline ll read() { ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } //************************************************************************************** ll a[100010],dp[100010]; int main() { int n=read(); for(int i=0;i<n;i++) { int x=read(); a[x]++; } for(int i=2;i<100010;i++) dp[i]=max(dp[i-1],dp[i-2]+a[i-1]*(i-1)); cout<<dp[100009]<<endl; }