Coderforces-455A
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.
Input
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).
Output
Print a single integer — the maximum number of points that Alex can earn.
Example
2
1 2
2
3
1 2 3
4
9
1 2 1 3 2 2 2 2 3
10
Note
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个数。你可选择大小的为A的数,但是A-1和A+1必须从数队移走。
题解:本题与数的顺序无关(这时候想到了DP),我们先对其排序。然后找其状态转移方程。假设dp[j]表示从1加到j 时最大值。则dp[j]=max(dp[j-1],dp[j-2]+i*cnt[i]).【ps:cnt[i]表示数值为】
AC代码为:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
long long n, a[100002], cot[100002], d[100002];
int main()
{
cin >> n;
long long Max = 0;
for (int i = 1; i <= n; i++)
{
cin >> a[i];
if (a[i] > Max)
Max = a[i];
cot[a[i]]++;
}
d[1] = cot[1]; d[0] = 0;
for (int i = 2; i <= Max; i++)
d[i] = max(d[i - 1], d[i - 2] + cot[i] * i);
cout << d[Max] << endl;
return 0;
}
Coderforces-455A的更多相关文章
- coderforces #387 Servers(模拟)
Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- coderforces #384 D Chloe and pleasant prizes(DP)
Chloe and pleasant prizes time limit per test 2 seconds memory limit per test 256 megabytes input st ...
- coderforces 731c
题目大意:给出m组数据,每组数据包括两个数Li与Ri,分别表示左右袜子的索引(下标),表示这一天要穿的袜子:而我们要使得每天穿的这两只袜子的颜色相同,所以可以改变袜子的颜色,每次只能改变一只袜子的颜色 ...
- coderforces 721b
题目描述: B. Passwords time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CoderForces 280B(记忆化搜索)
题目大意:一个纸牌游戏,52张纸牌排成一列,每张纸牌有面值和花色两种属性.每次操作可以用最后一张纸牌将倒数第二张或者倒数第四张替换,但前提是两张牌的花色或者面值相同.问最终能否只剩一张牌. 题目分析: ...
- CodeForces 455A Boredom (DP)
Boredom 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/G Description Alex doesn't like b ...
- Codeforces 455A Boredom (线性DP)
<题目链接> 题目大意:给定一个序列,让你在其中挑选一些数,如果你选了x,那么你能够得到x分,但是该序列中所有等于x-1和x+1的元素将全部消失,问你最多能够得多少分. 解题分析:从小到大 ...
- Codeforces 455A - Boredom - [DP]
题目链接:https://codeforces.com/problemset/problem/455/A 题意: 给出一个 $n$ 个数字的整数序列 $a[1 \sim n]$,每次你可以选择一个 $ ...
- CF 455A Boredom
A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- CoderForces 689A Mike and Cellphone (水题)
题意:给定一个手机键盘数字九宫格,然后让你判断某种操作是不是唯一的,也就是说是不是可以通过平移也能实现. 析:我的想法是那就平移一下,看看能实现,就四种平移,上,下,左,右,上是-3,要注意0变成8, ...
随机推荐
- 微服务SpringCloud之GateWay熔断、限流、重试
纯洁的微笑的Spring Cloud系列博客终于学完了,也对Spring Cloud有了初步的了解. 修改请求路径的过滤器 StripPrefix Filter 是一个请求路径截取的功能,我们可以利用 ...
- PHP经典算法题
1.百钱买百鸡 公鸡5文钱一只,母鸡3文钱一只,小鸡3只一文钱,用100文钱买一百只鸡,其中公鸡,母鸡,小鸡都必须要有,问公鸡,母鸡,小鸡要买多少只刚好凑足100文钱. 分析:估计现在小学生都能手工推 ...
- MyBatis直接执行sql语句mapper
<select id="queryBySql" resultType="HashMap"> <![CDATA[ ${sql} ]]> & ...
- ArcGIS API For Javascript :如何解决跨网不能正常获取依赖项的问题?
一.前言 政企项目通常会在自组网以及保密网运行,有些单位甚至会有两个物理隔绝的网络存在.通常情况下我们会在两个网络中部署相同的地图服务和依赖项.但是也有其中一个网络密级很高没有服务器资源,不能单独部署 ...
- 力扣(LeetCode)字符串中的第一个唯一字符 个人题解
给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcod ...
- HTML_本地存储
在HTML5当中,新增了很多的存储方式,这里我先介绍两种,方便我们的使用和操作,具体新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问 ...
- ubuntu 16.04 和 windows 10系统安装mysql 允许远程访问 | mysql user guide on ubuntu 16.04 and windows 10
本文首发于个人博客https://kezunlin.me/post/36e618e7/,欢迎阅读! mysql user guide on ubuntu 16.04 and windows 10 Pa ...
- Web Deploy远程发布
前言 我们在使用VS开发.net网站的时候,部署时可能会遇到缺少dll的问题,每次都远程桌面登陆,然后拷贝过去,太麻烦了.我们可以使用Web Deploy这个远程部署工具,不仅部署容易了,也方便进行迭 ...
- Openlayers Projection导致经纬度颠倒问题
问题: openlayers3调用TileWMS接口,实现Openlayers加载Geoserver转发的ArcGIS切片时,web墨卡托(wkid3857)没有问题,但是WGS84(wkid4326 ...
- 4sql
在 MySQL 中,有三种主要的类型:文本.数字和日期/时间类型. Text 类型:CHAR(size)VARCHAR(size)TINYTEXTTEXT 存放最大长度为 65,535 个字符的字符串 ...