难度 1500
题目 CodeForces:
Boredom time limit per test 1 second memory limit per test 256 megabytesAlex 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.
InputThe 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).
OutputPrint a single integer — the maximum number of points that Alex can earn.
KeyWord
boredom 无聊
perfectionist 完美主义者
题目解析
本题大意是对给出的n个数进行操作,使得最后得到的分数尽可能高,操作包括去掉一个元素,然后去掉数组内等于该元素-1和+1的所有元素,并使分数加上该元素的值。
这题是简单的dp,所以直接看代码
#include<iostream> #include<algorithm> using namespace std; typedef long long ll; ll x[100005] = { 0 }, y[100005] = { 0 }; ll n,ans; int main() { cin >> n; for (int i = 0; i < n; i++) { int t; cin >> t; x[t]++; } y[0] = 0; y[1] = x[1]; for (int i = 2; i < 100005; i++) { y[i] = max(y[i - 1], y[i - 2] + x[i] * i); ans = max(ans, y[i]); } cout << ans<<endl; return 0; }