题面描述

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\)之间的关系。这样我们就很容易得到递推式:

\[dp[i]=\begin{cases}
0 && i=0\\
num[1]*1 && i=1\\
max(dp[i-1],dp[i-2]+num[i]*i) && else
\end{cases}
\]

注意,最后一重for循环要从2循环至已知的maxn

  1. #include<bits/stdc++.h>
  2. #define maxn 1000050
  3. using namespace std;
  4. inline char get(){
  5. static char buf[3000],*p1=buf,*p2=buf;
  6. return p1==p2 && (p2=(p1=buf)+fread(buf,1,3000,stdin),p1==p2)?EOF:*p1++;
  7. }
  8. inline long long read(){
  9. register char c=getchar();register long long f=1,_=0;
  10. while(c>'9' || c<'0')f=(c=='-')?-1:1,c=getchar();
  11. while(c<='9' && c>='0')_=(_<<3)+(_<<1)+(c^48),c=getchar();
  12. return _*f;
  13. }
  14. long long note,n,a[maxn],dp[maxn];
  15. long long op=0;
  16. int main(){
  17. //freopen("1.txt","r",stdin);
  18. n=read();
  19. for(register long long i=1;i<=n;i++)a[i]=read(),dp[a[i]]+=a[i],note=max(note,a[i]);
  20. 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);
  21. cout<<op<<endl;
  22. return 0;
  23. }

[CodeForce455A]Boredom的更多相关文章

  1. CF456C Boredom (DP)

    Boredom CF#260 div2 C. Boredom Codeforces Round #260 C. Boredom time limit per test 1 second memory ...

  2. 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 ...

  3. CodeForces 455A Boredom (DP)

    Boredom 题目链接: http://acm.hust.edu.cn/vjudge/contest/121334#problem/G Description Alex doesn't like b ...

  4. cf455A Boredom

    A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  5. 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 ...

  6. Boredom

    Alex doesn't like boredom. That's why whenever he gets bored, he comes up with games. One long winte ...

  7. [Codeforces Round #433][Codeforces 853C/854E. Boredom]

    题目链接:853C - Boredom/854E - Boredom 题目大意:在\(n\times n\)的方格中,每一行,每一列都恰有一个被标记的方格,称一个矩形为漂亮的当且仅当这个矩形有两个角是 ...

  8. CodeForces 456-C Boredom

    题目链接:CodeForces -456C Description Alex doesn't like boredom. That's why whenever he gets bored, he c ...

  9. CF 455A Boredom

    A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

随机推荐

  1. IIS中启用CGI来托管PHP和Python网站

    IS中如果要托管PHP和Python的web站点,需要用到CGI来处理HTTP请求,默认情况IIS中是没有CGI模块的,需要在Windows server的服务器管理器(Server Manager) ...

  2. java模拟浏览器发送请求

    package test; import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.IOExcep ...

  3. flask总结之session,websocket,上下文管理

    1.关于session flask是带有session的,它加密后存储在用户浏览器的cookie中,可以通过app.seesion_interface源码查看 from flask import Fl ...

  4. 课时46.label标签(掌握)

    我们点击QQ注册页面,发现了一个问题,当我们点击密码两个字的时候,输入框聚焦了,而点击确认密码的时候,输入框也聚焦了,而我们上节课做的页面,这么点击,并不聚焦 1.默认情况下文字和输入框是没有关联关系 ...

  5. mysql获取正在运行的sql

    select id,db,host,time,info,command from information_schema.processlist where command<>'sleep' ...

  6. python3带tkinter窗口的ftp服务器,并使用pyinstaller打包成exe

    python带tkinter窗口的ftp服务器,使用python3编写,打包使用pyinstaller,命令 pyinstaller -F .\ftpserver.py 代码也可在我的github上下 ...

  7. AJAX上传文件到服务器

    上传文件是常要处理的事情,使用ajaxFileUpload.js处理比较方便,这里的ajaxFileUpload.js文件修改过的, Html部分 <input type="file& ...

  8. you don't have permission to access ...........on this server问题解决

    因为刚刚开始使用新架构的项目,于是把老项目统统替换成了新的项目.配置好后,本地登录页面报 you don't have permission to access ...... on this serv ...

  9. 用原生JS写一个网页版的2048小游戏(兼容移动端)

    这个游戏JS部分全都是用原生JS代码写的,加有少量的CSS3动画,并简单的兼容了一下移动端. 先看一下在线的demo:https://yuan-yiming.github.io/2048-online ...

  10. 4 二维数组中的查找 JavaScript

    题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...