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;
    }
    




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

  • 相关阅读:
    java web中使用log4j
    Apache Log4j配置说明
    sql数据库为null时候ASP语句判断问题
    js实现两个文本框数值的加减乘除运算
    js实现文本框支持加减运算的方法
    php报错syntax error, unexpected T_GOTO, expecting T_STRING,报错文件与行数指向以下代码,是什么原因?
    安装DEDECMS出现Function ereg_replace()错误的解决方法
    按钮显示隐藏div、input等
    设计input搜索框提示文字点击消失的效果
    Xcode export/upload error: Your session has expired. Please log in-b
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4643514.html
Copyright © 2011-2022 走看看