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 ...
随机推荐
- python字符串内置函数
1.字符串 定义:它是一个有序的字符的集合,用于存储和表示基本的文本信息,‘’或“”或‘’‘ ’‘’中间包含的内容称之为字符串特性:1.只能存放一个值2.不可变3.按照从左到右的顺序定义字符集合,下标 ...
- vue中的表单异步校验方法封装
在vue项目的开发中,表单的验证必不可少,在开发的过程中,用的是vue+iview的一套,我们知道iview的表单验证是基于async-validator,对于async-validator不熟悉的可 ...
- SVG(可缩放矢量图形)
SVG可缩放矢量图形(Scalable Vector Graphics)是基于可扩展标记语言(XML),用于描述二维矢量图形的一种图形格式.SVG是W3C("World Wide W ...
- hdu 2059 龟兔赛跑(动态规划DP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2059 龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) M ...
- Java面试基础知识1
1.动态绑定是指在执行期间判断所引用对象的实际类型,根据其实际的类型调用其相应的方法. 2.在将超类转换为子类之前,应该使用instanceof进行检查. 3.包含一个或者多个抽象方法的类本身必须被声 ...
- 【Android XML】Android XML 转 Java Code 系列之 介绍(1)
最近在公司做一个项目,需要把Android界面打包进jar包给客户使用.对绝大部分开发者来说,Android界面的布局以XML文件为主,并辅以少量Java代码进行动态调整.而打包进jar包的代码,意味 ...
- python基础===self的理解
self是类的实例 self有点类似java中的this,无实际意义.但是约定俗成的都是用self表示类的实例 class A: def func(self): print(self) #指向的是类的 ...
- OC 07 类的扩展
1.NSDate的使用 NSDate是Cocoa中⽤于处理⽇期和时间的基础类,封装了某⼀给定的时刻(含日期,时间,时区) 注意NSLog(@“%@”,nowDate);⽆论你是哪个时区的时间,打印时总 ...
- Leetcode 之Length of Last Word(38)
做法很巧妙.分成左右两个对应的部分,遇到左半部分则入栈,遇到右半部分则判断对应的左半部分是否在栈顶.注意最后要判断堆栈是否为空. bool isValid(const string& s) { ...
- 同步方法-java
除了同步代码块能实现同步执行外,同步方法也可以. 先看下同步代码块实现同步执行: public class Demo4 { public static void main(String[] args) ...