[CodeForce455A]Boredom
题面描述
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.
给定一个由n个整数组成的序列。玩家可以做几个步骤。在单个步骤中,他可以选择序列的元素(假设为\(a_k\))并删除它,此时,所有等于\(a_k+1和a_k-1\)的元素也必须从序列中删除。这个步骤给玩家带来\(a_k\)点数。
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).
第一行包含一个整数n(\(1≤n≤105\)),表示Alex序列中有多少个数字。
第二行包含n个整数\(a1,a2,…,an(1≤105)\)
输出格式
输出一个整数——Alex可以获得的最大点数
样例
样例输入
9
1 2 1 3 2 2 2 2 3
样例输出
10
题解
先求出数列中每一个数字k的出现次数num[k]
考虑取任意一个数\(x\)时只会影响到\(x+1\)和\(x-1\),我们可以先设dp[i]表示选取num后可以取得的最大值。因为任意取两个数\(a和b\),若选取\(a\)后可以选取\(b\),则选取\(b\)后可以选取\(a\),因此我们只考虑\(x与x-1\)之间的关系。这样我们就很容易得到递推式:
0 && i=0\\
num[1]*1 && i=1\\
max(dp[i-1],dp[i-2]+num[i]*i) && else
\end{cases}
\]
注意,最后一重for循环要从2循环至已知的maxn
#include<bits/stdc++.h>
#define maxn 1000050
using namespace std;
inline char get(){
static char buf[3000],*p1=buf,*p2=buf;
return p1==p2 && (p2=(p1=buf)+fread(buf,1,3000,stdin),p1==p2)?EOF:*p1++;
}
inline long long read(){
register char c=getchar();register long long f=1,_=0;
while(c>'9' || c<'0')f=(c=='-')?-1:1,c=getchar();
while(c<='9' && c>='0')_=(_<<3)+(_<<1)+(c^48),c=getchar();
return _*f;
}
long long note,n,a[maxn],dp[maxn];
long long op=0;
int main(){
//freopen("1.txt","r",stdin);
n=read();
for(register long long i=1;i<=n;i++)a[i]=read(),dp[a[i]]+=a[i],note=max(note,a[i]);
for(register long long i=2;i<=note;i++)dp[i]=max(dp[(i)-1],dp[(i)-2]+dp[i]),op=max(dp[i],op);
cout<<op<<endl;
return 0;
}
[CodeForce455A]Boredom的更多相关文章
- CF456C Boredom (DP)
Boredom CF#260 div2 C. Boredom Codeforces Round #260 C. Boredom time limit per test 1 second memory ...
- Codeforces Round #260 (Div. 1) A - Boredom DP
A. Boredom Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/455/problem/A ...
- CodeForces 455A Boredom (DP)
Boredom 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/G Description Alex doesn't like b ...
- cf455A Boredom
A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Codeforces Round #260 (Div. 2)C. Boredom(dp)
C. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- Boredom
Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winte ...
- [Codeforces Round #433][Codeforces 853C/854E. Boredom]
题目链接:853C - Boredom/854E - Boredom 题目大意:在\(n\times n\)的方格中,每一行,每一列都恰有一个被标记的方格,称一个矩形为漂亮的当且仅当这个矩形有两个角是 ...
- CodeForces 456-C Boredom
题目链接:CodeForces -456C Description Alex doesn't like boredom. That's why whenever he gets bored, he c ...
- CF 455A Boredom
A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
随机推荐
- checkout 提示失败
事故现场有些时候,当我们使用 git checkout <branchname>命令切换分支的时候,有时会切换失败,然后出现以下的提示信息: 提示信息说的很清楚,当前分支有未跟踪的文件,c ...
- 安装jdk1.7
1.压缩文件放到/usr文件夹里 2.解压到 /usr里,tar -zxvf jdk-7u71-linux-i586.tar.gz 3.配置jdk环境变量,打开/etc/profile配置文件,将下面 ...
- HDU 2021 发工资咯:)(最水贪心)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2021 发工资咯:) Time Limit: 2000/1000 MS (Java/Others) ...
- PHP实现数组递归转义的方法
本文以实例形式讲述了PHP实现数组递归转义的方法,分享给大家供大家参考之用.具体方法如下: 主要功能代码如下: $arr = array('a"aa',array("c'd&quo ...
- 浅谈React、Vue 部分异步
React中的setState setState为什么需要异步? 无法限制何时使用异步,多次连续使用setState 防止多次渲染,异步rendering不仅仅是性能上的优化,而且这可能是react组 ...
- 走进MySQL
MySQL 是最流行的关系型数据库管理系统,在 WEB 应用方面 MySQL 是最好的 RDBMS(Relational Database Management System:关系数据库管理系统)应用 ...
- vue 引入 mint-ui 简单使用
一 npm 方式 1,安装依赖 (已有项目) 如果想简单体验:基于vue-cli /* npm install vue -g npm install vue-cli -g // -g 是否全局 ...
- shell入门基础&常见命令及用法
shell shell是一个命令解释器,实际是一个程序,/bin/bash,linux中所有的命令都由它来解释,有自己的语法 shell脚本 以.sh结尾 shell语法+linux命令 注释: 单行 ...
- MYSQL小函数大用途之-------FIND_IN_SET
没有前言和解释,直接看怎么用 当前我所知道两种用法: 第一种:和like的作用有点相似,但用这个函数会比like更准确的查到你想要的数据. 前提是当前的字段满足俩个要求: 类型为字符型. 储存格式为- ...
- php5.4以上 mysqli 实例操作mysql 增,删,改,查
<?php //php5.4以上 mysqli 实例操作mysql header("Content-type:text/html;charset=utf8"); $conn ...