首页 > 其他 > 详细

Boredom CodeForces - 455A

时间:2021-06-30 20:56:37      阅读:33      评论:0      收藏:0      [点我收藏+]

原题链接
考察:线性dp(状态机dp?)
错误思路:
??对于每个数字按出现次数sz与值val的乘积排序,假设第一个值为x,\(x*sz[x]>(x-1)*sz[x-1]\)但这并不表明\((x-1)*sz[x-1]+(x+1)*sz[x+1]会<x*sz[x]\).所以贪心是不可取的.
正确思路:
??选择没有规律,考虑dp或者dfs.很明显可以用dp,f[i][1/0]表示值为i的数选不选的最大价值.

Code

#include <iostream> 
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
const int N = 100010;
int sz[N],n,maxn;
LL f[N][2];
int main()
{
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int x; scanf("%d",&x);
		sz[x]++;
		maxn = max(x,maxn);
	}
	for(int i=1;i<=maxn;i++)
	{
		f[i][0] = max(f[i-1][1],f[i-1][0]);
		f[i][1] = f[i-1][0]+(LL)sz[i]*i;
	}
	printf("%lld\n",max(f[maxn][0],f[maxn][1]));
	return 0;
}

Boredom CodeForces - 455A

原文:https://www.cnblogs.com/newblg/p/14956089.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!