Codeforces Round #260 (Div. 2)C. Boredom(dp)
1 second
256 megabytes
standard input
standard 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.
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).
Print a single integer — the maximum number of points that Alex can earn.
2
1 2
2
3
1 2 3
4
9
1 2 1 3 2 2 2 2 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.
给出n个数,每次能够选择消除一个值为ai的数,那么全部ai-1。ai+1的数也会被消掉,同一时候会获得值ai,问最多能够获得多少?
看完题就可想到这应该是一个dp问题,首先,哈希一下,存下每一个数的个数放在p中,消除一个数i,会获得p[i]*i的值(由于能够消除p[i]次),假设从0的位置開始向右消去,那么,消除数i时,i-1可能选择了消除,也可能没有。假设消除了i-1,那么i值就已经不存在,dp[i] = dp[i-1]。假设没有被消除。那么dp[i] = dp[i-2]+ p[i]*i。
那么初始的dp[0] = 0 ; dp[1] = p[1] ;得到了公式 dp[i] = max(dp[i-1],dp[i-2]+p[i]*i).
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL __int64
using namespace std;
LL p[110000] , dp[110000] ;
int main()
{
LL i , n , x , maxn = -1;
memset(p,0,sizeof(p));
memset(dp,0,sizeof(dp));
scanf("%I64d", &n);
for(i = 1 ; i <= n ; i++)
{
scanf("%I64d", &x);
if(x > maxn)
maxn = x ;
p[x]++ ;
}
dp[1] = p[1] ;
for(i = 2 ; i <= maxn ; i++)
{
dp[i] = max( dp[i-1],dp[i-2]+p[i]*i );
}
printf("%I64d\n", dp[maxn]);
return 0;
}
Codeforces Round #260 (Div. 2)C. Boredom(dp)的更多相关文章
- Codeforces Round #260 (Div. 1) A. Boredom (简单dp)
题目链接:http://codeforces.com/problemset/problem/455/A 给你n个数,要是其中取一个大小为x的数,那x+1和x-1都不能取了,问你最后取完最大的和是多少. ...
- Codeforces Round #658 (Div. 2) D. Unmerge(dp)
题目链接:https://codeforces.com/contest/1382/problem/D 题意 给出一个大小为 $2n$ 的排列,判断能否找到两个长为 $n$ 的子序列,使得二者归并排序后 ...
- Codeforces Round #471 (Div. 2) F. Heaps(dp)
题意 给定一棵以 \(1\) 号点为根的树.若满足以下条件,则认为节点 \(p\) 处有一个 \(k\) 叉高度为 \(m\) 的堆: 若 \(m = 1\) ,则 \(p\) 本身就是一个 \(k\ ...
- 【Codeforces】Codeforces Round #374 (Div. 2) -- C. Journey (DP)
C. Journey time limit per test3 seconds memory limit per test256 megabytes inputstandard input outpu ...
- Codeforces Round #652 (Div. 2) D. TediousLee(dp)
题目链接:https://codeforces.com/contest/1369/problem/D 题意 最初有一个结点,衍生规则如下: 如果结点 $u$ 没有子结点,添加 $1$ 个子结点 如果结 ...
- Codeforces Round #247 (Div. 2) C. k-Tree (dp)
题目链接 自己的dp, 不是很好,这道dp题是 完全自己做出来的,完全没看题解,还是有点进步,虽然这个dp题比较简单. 题意:一个k叉树, 每一个对应权值1-k, 问最后相加权值为n, 且最大值至少为 ...
- Codeforces Round #165 (Div. 1) Greenhouse Effect(DP)
Greenhouse Effect time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Round #119 (Div. 2) Cut Ribbon(DP)
Cut Ribbon time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- DP Codeforces Round #260 (Div. 1) A. Boredom
题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...
随机推荐
- libcurl的使用问题“Expect100-continue”
最近在做团购酒店APP分享到qzone功能,使用libcurl访问qzone的分享cgi接口,酒店分享信息以POST方式传输,在测试的时候发现分享接口平均有2s的延迟,这延迟也太大了吧,于是乎问了空间 ...
- Shared_from_this 几个值得注意的地方
shared_from_this()是enable_shared_from_this<T>的成员 函数,返回shared_ptr<T>.首先需要注意的是,这个函数仅在share ...
- Linux 下的多线程编程
随着你对编程的深入,多线程是一个免不了的话题,在这里就对多线程做一个比较详细的总结. 首先摆在我们面前的就是什么是线程,以及为么会有这个东西.记得之前学习的时候自己会画一张很大的图,在图中可以详细的写 ...
- hdoj 2544 最短路(最短路+Dijkstrea算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路分析:该问题给定一个无向图,要求求从起始点到终点的最短路径长度:可以使用dijkstra算法 ...
- hdoj 5311 Hidden String(KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5311 思路分析:该问题要求在字符串中是否存在三个不相交的子串s[l1..r1], s[l2..r2], ...
- [LeetCode] Longest Substring Without Repeating Characters (LinkedHashSet的妙用)
Given a string, find the length of the longest substring without repeating characters. For example, ...
- VisualSVN Server安装后,TortoiseSVN远程无法访问版本库。
修正!重演了一遍,发现总结有误,重新整理下.首先访问版本库的路径不清楚的话可以在VisualSVN Server的版本库上右键“Copy URL to Clipboard”.访问版本库失败的几种情况: ...
- [C++参考]私有成员变量的理解
私有成员变量的概念,在脑海中的现象是,以private关键字声明,是类的实现部分,不对外公开,不能在对象外部访问对象的私有成员变量. 然而,在实现拷贝构造函数和赋值符函数时,在函数里利用对象直接访问了 ...
- codeforces 463E . Caisa and Tree
题目链接 给一棵树, 两种操作, 一种是将点u的权值改为y, 另一种是查询根节点到点u的路径上, gcd(v, u)>1的深度最深的点v. 修改操作不超过50次. 这个题, 暴力可以过, 但是在 ...
- c 占位符
%d, %i,代表整数,%f-浮点,%s,字符串,%c,char. %p 指针,%fL 长log,%e科学计数,%g 小数或科学计数. C语言中的格式占位符: %a,%A 读入一个浮点值(仅C9 ...