zoukankan      html  css  js  c++  java
  • CF 455A(Boredom-dp)


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

    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 a1a2, ..., an (1 ≤ ai ≤ 105).

    Output

    Print a single integer — the maximum number of points that Alex can earn.

    Sample test(s)
    input
    2
    1 2
    
    output
    2
    
    input
    3
    1 2 3
    
    output
    4
    
    input
    9
    1 2 1 3 2 2 2 2 3
    
    output
    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.


    Dp,设数i出现a[i]次

    非常easy发现,从n取到i(i全取)的最优值f{i}仅仅与f(i+1)和f(i+2)相关



    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<algorithm>
    #include<functional>
    #include<iostream>
    #include<cmath>
    #include<cctype>
    #include<ctime>
    using namespace std;
    #define For(i,n) for(int i=1;i<=n;i++)
    #define Fork(i,k,n) for(int i=k;i<=n;i++)
    #define Rep(i,n) for(int i=0;i<n;i++)
    #define ForD(i,n) for(int i=n;i;i--)
    #define RepD(i,n) for(int i=n;i>=0;i--)
    #define Forp(x) for(int p=pre[x];p;p=next[p])
    #define Lson (x<<1)
    #define Rson ((x<<1)+1)
    #define MEM(a) memset(a,0,sizeof(a));
    #define MEMI(a) memset(a,127,sizeof(a));
    #define MEMi(a) memset(a,128,sizeof(a));
    #define INF (2139062143)
    #define F (100000007)
    #define MAXN (100000+10)
    long long mul(long long a,long long b){return (a*b)%F;}
    long long add(long long a,long long b){return (a+b)%F;}
    long long sub(long long a,long long b){return (a-b+(a-b)/F*F+F)%F;}
    typedef long long ll;
    int n,a[MAXN]={0};
    ll f[MAXN]={0};
    int main()
    {
    //	freopen("seq.in","r",stdin);
    //	freopen(".out","w",stdout);
    	MEM(a)
    	cin>>n;
    	int p,m=0;
    	For(i,n)
    	{
    		scanf("%d",&p);
    		a[p]++;
    		m=max(m,p);
    	}
    	ll ans=0;
    	ForD(i,m)
    	{
    		f[i]=max(f[i+1],f[i+2]+(ll)i*a[i]);
    		ans=max(ans,f[i]);
    	}
    	cout<<ans<<endl;
    	
    	
    	return 0;
    }
    




    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    solr一些错误
    maven聚合工程之找不到符号,类等错误
    图片服务器搭建及上传测试,网络配置问题(errno: 111)
    安装Nginx,make时报错:/usr/bin/ld: cannot find -lfdfsclient或者类似找不到-lxxx
    关于PCA算法的学习
    生物信息及开发常用的Linux命令
    05.课程主页面三个接口开发
    04.增加media文件配置
    3.课程相关5张表设计
    2.增加抽象基类
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4643514.html
Copyright © 2011-2022 走看看