有明显的递推关系:

f[i]表示i为数列中最大值时所求结果。num[i]表示数i在数列中出现了几次。

对于数i,要么删i,要么删i-1,只有这两种情况,且子问题还是一样的思路。那么很显然递推一下就行了:f[i]=max(f[i-1],f[i-2]+i*num[i]);

这里技巧在于:为了防止麻烦,干脆就所有数的出现次数都记录一下,然后直接从2推到100000(类似于下标排序),就不用排序了,也不用模拟删除操作了。这一技巧貌似简单,但实际上临场想出来也需要点水平。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
#define INF 1000000000
#define eps 1e-8
#define pii pair<int,int>
#define LL long long int
LL f[],num[];
int n,x;
int main()
{
//freopen("in3.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&x);
num[x]++;
}
f[]=num[];f[]=;
for(LL i=;i<=;i++)
{
f[i]=max(f[i-],f[i-]+i*num[i]);
}
printf("%I64d\n",f[]);
//fclose(stdin);
//fclose(stdout);
return ;
}

Codeforces Round #260(div2)C(递推)的更多相关文章

  1. codeforces Round #260(div2) D解决报告

    D. A Lot of Games time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  2. 递推DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* DP:从1到最大值,dp[i][1/0] 选或不选,递推更新最大值 */ #include <cstdio> #include <algorithm> #in ...

  3. Codeforces Round #539 div2

    Codeforces Round #539 div2 abstract I 离散化三连 sort(pos.begin(), pos.end()); pos.erase(unique(pos.begin ...

  4. 【前行】◇第3站◇ Codeforces Round #512 Div2

    [第3站]Codeforces Round #512 Div2 第三题莫名卡半天……一堆细节没处理,改一个发现还有一个……然后就炸了,罚了一啪啦时间 Rating又掉了……但是没什么,比上一次好多了: ...

  5. DP Codeforces Round #260 (Div. 1) A. Boredom

    题目传送门 /* 题意:选择a[k]然后a[k]-1和a[k]+1的全部删除,得到点数a[k],问最大点数 DP:状态转移方程:dp[i] = max (dp[i-1], dp[i-2] + (ll) ...

  6. Codeforces Round#320 Div2 解题报告

    Codeforces Round#320 Div2 先做个标题党,骗骗访问量,结束后再来写咯. codeforces 579A Raising Bacteria codeforces 579B Fin ...

  7. Codeforces Round #564(div2)

    Codeforces Round #564(div2) 本来以为是送分场,结果成了送命场. 菜是原罪 A SB题,上来读不懂题就交WA了一发,代码就不粘了 B 简单构造 很明显,\(n*n\)的矩阵可 ...

  8. codeforces D. Queue 找规律+递推

    题目链接: http://codeforces.com/problemset/problem/353/D?mobile=true H. Queue time limit per test 1 seco ...

  9. Codeforces Round #382 (div2)

    A:题目:http://codeforces.com/contest/735/problem/A 题意:出发点G,终点T,每次只能走k步,#不能走,问能否到达终点 思路:暴力 #include < ...

随机推荐

  1. Logstash简介

    支持多种数据获取机制,通过TCP/UDP协议.文件.syslog.windows.EventLogs及STDIN等:获取到数据口,支持对数据进行过滤.修改等操作JRuby语言研发,工作在JVM中   ...

  2. pandas(一)操作Series和DataFrame的基本功能

    reindex:重新索引 pandas对象有一个重要的方法reindex,作用:创建一个适应新索引的新对象 以Series为例 >>> series_obj = Series([4. ...

  3. 3.6中的range()

    在python3中range()是这样的: >>> range(4) range(0, 4) #额,列表跑哪去了 在之前的python2中是这样的: >>> ran ...

  4. settings配置 文件操作

    设置文件路径 import os BASE_DIR = os.path.dirname(os.path.dirname(__file__)) 这里用到了python中一个神奇的变量 __file__ ...

  5. 如何成为专业的PHP开发者

    如何才能成为一名专业的PHP开发者?资深Web开发者Bruno Skvorc在其博客上分享了一些心得. 当阅读各种和PHP相关的博客.Quora问题.Google+社区.资讯和杂志的时候,Bruno ...

  6. PAT 天梯赛 L2-024. 部落 【并查集】

    题目链接 https://www.patest.cn/contests/gplt/L2-024 题意 给出 几个不同的圈子,然后 判断 有哪些人 是属于同一个部落的,或者理解为 ,有哪些人 是有关系的 ...

  7. qnx 黑莓系统

    qnx 黑莓图形系统框架 http://www.360doc.com/content/07/0807/10/38907_658192.shtml http://blog.csdn.net/keyboa ...

  8. U-Boot中支持USB

    转载: http://blog.csdn.net/qiurihuanghua/article/details/6234832 今天查看了一下在P4080DS板子的U-Boot中支持USB,主要是加入U ...

  9. mysql慢查询设置

    不同版本的mysql命令和配置不一样,以下是2个版本 修改配置文件 log-slow-queries=/alidata/mysql-log/mysql-slow.log long_query_time ...

  10. java深入探究12-框架之Hibernate

    1.引入SSH框架 Struts框架,基于MVC 模式的应用层框架技术 Hibernate,基于持久层框架(数据访问层使用) Dao代码编写的几种方式: 1.原始jdbc操作,Connection/S ...