Codeforces Round #260 (Div. 1) 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.
【题意】给你一个序列,现在要将所有的数删除。如果删除了a[k],则a[k]+1和a[k]-1都将被删除,此次删除的收益为a[k]。求最大收益。
【分析】DP。我们将数字1~maxn依次遍历,dp[i]表示当前数字获得的最大收益,则有两种情况。一:i这个数字在数组中没有,则
dp[i]=i-2<0?0:dp[i-2];二:这个数字在数组中存在,则他可能从i-2的数字那遍历过来,也有可能从i-3的地方遍历过来,继续更新就行了,然后在删除最后一个或者倒数第二个的时候取一下最大值就行了。
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define vi vector<int>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int N = 1e5+;
int n,maxn;
int a[N],cnt[N];
LL dp[N];
int main(){
scanf("%d",&n);
for(int i=,x;i<=n;i++){
scanf("%d",&a[i]);
maxn=max(maxn,a[i]);
cnt[a[i]]++;
}
LL ans=;
for(int i=;i<=maxn;++i){
if(!cnt[i]){
dp[i]=i-<?:dp[i-];
if(i>=maxn-)ans=max(ans,dp[i]);
continue;
}
dp[i]=1LL*((i-<?:dp[i-])+1LL*cnt[i]*i);
dp[i]=max(dp[i],1LL*((i-<?:dp[i-])+1LL*cnt[i]*i));
//printf("i:%d dpi:%lld\n",i,dp[i]);
if(i>=maxn-)ans=max(ans,dp[i]);
}
printf("%lld\n",ans);
return ;
}
Codeforces Round #260 (Div. 1) Boredom(DP)的更多相关文章
- Codeforces Round #306 (Div. 2) ABCDE(构造)
A. Two Substrings 题意:给一个字符串,求是否含有不重叠的子串"AB"和"BA",长度1e5. 题解:看起来很简单,但是一直错,各种考虑不周全, ...
- Codeforces Round #598 (Div. 3)E(dp路径转移)
题:https://codeforces.com/contest/1256/problem/E 题意:给一些值,代表队员的能力值,每组要分3个或3个以上的人,然后有个评价值x=(队里最大值-最小值), ...
- Codeforces Round #523 (Div. 2)C(DP,数学)
#include<bits/stdc++.h>using namespace std;long long a[100007];long long dp[1000007];const int ...
- Codeforces Round #309 (Div. 1) A(组合数学)
题目:http://codeforces.com/contest/553/problem/A 题意:给你k个颜色的球,下面k行代表每个颜色的球有多少个,规定第i种颜色的球的最后一个在第i-1种颜色的球 ...
- Codeforces Round #392(Div 2) 758F(数论)
题目大意 求从l到r的整数中长度为n的等比数列个数,公比可以为分数 首先n=1的时候,直接输出r-l+1即可 n=2的时候,就是C(n, 2)*2 考虑n>2的情况 不妨设公比为p/q(p和q互 ...
- Codeforces Round #532 (Div. 2)- B(思维)
Arkady coordinates rounds on some not really famous competitive programming platform. Each round fea ...
- Codeforces Round #254 (Div. 2) B (445B)DZY Loves Chemistry
推理可得终于结果为2的(n-可分组合数)次方. 问题是怎么求出可分组合数,深搜就可以,当然并查集也能够. AC代码例如以下: 深搜代码!!! #include<iostream> #inc ...
- Codeforces Round #597 (Div. 2)D(最小生成树)
/*每个点自己建立一座发电站相当于向超级源点连一条长度为c[i]的边,连电线即为(k[i]+k[j])*两点间曼哈顿距离,跑最小生成树(prim适用于稠密图,kruscal适用于稀疏图)*/ #def ...
- Codeforces Round #327 (Div. 2)B(逻辑)
B. Rebranding time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
随机推荐
- 【C++ STL】List
1.结构 list使用一个double linked list(双向链表)来管理元素. 2. list 能力 list内部结构和vector或deque截然不同,所以与他们的区别: list不支持随机 ...
- linux sh脚本异常:/bin/sh^M:bad interpreter: No such file or directory
在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory.这是不同系统编码格式引起的:在windows系统中编辑的. ...
- bzoj 1776: [Usaco2010 Hol]cowpol 奶牛政坛——树的直径
农夫约翰的奶牛住在N (2 <= N <= 200,000)片不同的草地上,标号为1到N.恰好有N-1条单位长度的双向道路,用各种各样的方法连接这些草地.而且从每片草地出发都可以抵达其他所 ...
- Spring 事务管理(山东数漫江湖)
最新又重新学习了一遍Spring的事务,这里做点总结,不做如何一步步配置的流水账. 1. 关键类 public interface PlatformTransactionManager { Trans ...
- 取石子游戏 HDU2516(斐波那契博弈)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2516 题目: Problem Description 1堆石子有n个,两人轮流取.先取者第1次可以取任 ...
- Tensorflow 2.0.0-alpha 安装 Linux系统
1.TensorFlow2.0的安装测试 Linux Tensorflow Dev Summit 正式宣布 Tensorflow 2.0 进入 Alpha 阶段. 基于 Anaconda 创建环境一个 ...
- 表格td内容超出宽度显示... table-layout: fixed;
td宽度用百分比固定好的时候,即使设置了 white-space:nowrap;/*文本不会换行,在同一行显示*/ overflow:hidden;超出隐藏 text-overflow:ellipsi ...
- 变量对象vs活动对象
这是我见过描述的最为详尽的关于变量对象.活动对象以及闭包的解析,来自知乎,感谢答主: 作者:闭家锁链接:https://www.zhihu.com/question/36393048/answer/7 ...
- 1.Firedac开门篇
firedac是Delphi开发跨平台的数据库应用程序的通用数据访问组件,同样适用于C++ Builder和FreePascal.firedac可以高速直接访问: 1.InterBase 2.SQLi ...
- position:fixed部分版本的浏览器不支持
ie6-ie8浏览器不支持这个属性 .fixed{ position:fixed; /*对于火狐等其他浏览器需要设置的*/ top:700px; /*同上*/ ...