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 ...
随机推荐
- 2015/9/5 Python基础(9):条件和循环
条件语句Python中的if语句如下: if expression: expr_true_suite 其中expression可以用布尔操作符and, or 和 not实现多重判断条件.如果一个复合语 ...
- OScached缓存整个页面和缓存局部页面
1.缓存整个页面 在OSCache组件中提供了一个CacheFilter用于实现页面级的缓存.主要用于对web应用中的某些动态页面进行缓存,尤其是那些需要生成PDF格式文件/报表.图片文件等的页面,不 ...
- Spring Security 过滤器链
Alias Filter Class Namespace Element or Attribute CHANNEL_FILTER ChannelProcessingFilter http/interc ...
- 变量对象vs活动对象
这是我见过描述的最为详尽的关于变量对象.活动对象以及闭包的解析,来自知乎,感谢答主: 作者:闭家锁链接:https://www.zhihu.com/question/36393048/answer/7 ...
- [Leetcode Week12]Unique Paths II
Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...
- 使用XShell通过SSH访问Google谷歌云服务器方法
1:先用Xshell创建个密钥 下一步到这里,这个名称要记得,谷歌后台要用的. 把这里的公钥复制出来,当然最好也可以备份下. 2:到谷歌后台去添加ssh,然后就能连接了. 复制刚才生成的公钥,在谷歌云 ...
- mongo数据库基本操作--python篇
连接数据库 MongoClient VS Connection class MongoClient(pymongo.common.BaseObject) | Connection to MongoDB ...
- linux命令(39):ss命令
ss是Socket Statistics的缩写.顾名思义,ss命令可以用来获取socket统计信息,它可以显示和netstat类似的内容.但ss的优势在于它能够显示更多更详细的有关TCP和连接状态的信 ...
- linux命令(31):lsof命令
1.递归查看某个目录的文件信息: lsof test/test1 2.不使用+D选项,遍历查看某个目录的所有文件信息的方法 :lsof |grep 'test/test3' 3.列出某个用户打开的文 ...
- golang-指针,函数,map
指针 普通类型变量存的就是值,也叫值类型.指针类型存的是地址,即指针的值是一个变量的地址.一个指针只是值所保存的位置,不是所有的值都有地址,但是所有的变量都有.使用指针可以在无需知道变量名字的情况下, ...