CF 455A Boredom

这篇博客介绍了Codeforces的一道编程问题455A,涉及动态规划策略来解决如何从序列中选择元素以最大化得分。玩家每次选择一个元素并删除其相邻相同的元素,目标是获得最高分数。博客提供了问题描述、示例输入/输出以及解题思路。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接:http://codeforces.com/problemset/problem/455/A
A. Boredom
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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 ≤ 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.

Examples
inputCopy
2
1 2
outputCopy
2
inputCopy
3
1 2 3
outputCopy
4
inputCopy
9
1 2 1 3 2 2 2 2 3
outputCopy
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.

题目翻译:亚历克斯不喜欢无聊。这就是为什么每当他感到无聊的时候,他就会想出游戏来。一个漫长的冬夜,他想出了一场比赛,决定参加比赛。给定由n个整数组成的序列a。玩家可以做几个步骤。在一个步骤中,他可以选择序列中的一个元素(让我们将其表示为AK)并删除它,此时所有等于AK  1和AK - 1的元素也必须从序列中删除。这一步给玩家带来了AK积分。亚历克斯是个完美主义者,所以他决定尽可能多得积分。帮帮他。

解题思路:dp,取当前num为i的值作为积分,则i-1和i+1都不作为积分计算进去,再加上i-1之前已经排好的最佳积分情况,作为一种情况。第二种就是到了num为i时,i不作为积分计算进去,i是作为被删掉的数字,所以第二种情况直接就等于i-1的最佳积分情况。这两种情况取最大的情况,就是当前到了i的答案

#include<iostream>
#define ll long long
using namespace std;
ll n,num,cnt[100005],dp[100005];
int main()
{
	cin >> n;
	while(n--)
	{
		cin >> num;
		cnt[num]++;
	}
	dp[1] = cnt[1];
	for(int i =2;i<=100000;i++)
	{
		dp[i] = max(dp[i-1],dp[i-2] + cnt[i] * i);
		
	}
	cout<<dp[100000];
	return 0;
}```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值