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) ...
随机推荐
- Android中GPS简介及其应用
GPS是Global Positioning System(全球定位系统)的简称,它的作用就是为全球的物体提供定位功能.GPS定位是一门高新技术,但对于Android程序员来说,开发GPS功能的应用程 ...
- 根据选择项过滤GridView
前台代码: <div> <asp:CheckBoxList ID="CheckBoxList1" runat="server" AutoPos ...
- iOS离线打包
预备环境 iOS开发环境,Mac OS.XCode 7.2以上版本: 下载HBuilder离线打包iOS版SDK(5+ SDK下载). SDK目录说明 HBuilder-Hello:离线打包演示应用: ...
- gcc代码反汇编查看内存分布[2]: arm-linux-gcc
arm-none-linux-gnueabi-gcc -v gcc version 4.4.1 (Sourcery G++ Lite 2010q1-202) 重点: 代码中的内存分配, 地址从低到高: ...
- linux账户管理(centos)
1./etc/passwd 非常详细的/etc/passwd解释 http://luzl.iteye.com/blog/564404 vi /etc/passwd,可以看到如下信息,在最后一行可以看到 ...
- VC获取屏幕分辨率大小
//以下两个函数获取的是显示屏幕的大小,不包括任务栏等区域 int screenwidth=GetSystemMetrics(SM_CXFULLSCREEN); int screenheight=Ge ...
- 条款21: 必须返回对象时,不要强行返回对象的reference
总结: 绝不要返回一个local栈对象的指针或引用:绝不要返回一个被分配的堆对象的引用:绝不要返回一个静态局部对象(为了它,有可能同时需要多个这样的对象的指针或引用). 条款4中给出了“在单线程环境中 ...
- java 自定义BufferedReader_readLine
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import ...
- django 开发简易博客(二)
这一节我们来了解模板和视图.URL的使用. 一.使用模板 在blog目录中新建templates文件夹,在templates文件夹下新建base.html文件.目录结构如下 templates/ ba ...
- 我的Python成长之路---第六天---Python基础(20)---2016年2月20日(晴)
一.面向对象基础 面向对象名词解释: 类(Class): 用来描述具有相同的属性和方法的对象的集合.它定义了该集合中每个对象所共有的属性和方法.对象是类的实例. 类变量:类变量在整个实例化的对象中是公 ...