zoukankan      html  css  js  c++  java
  • CF 455A Boredom

    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.

    Examples
    input
    Copy
    2
    1 2
    
    output
    Copy
    2
    
    input
    Copy
    3
    1 2 3
    
    output
    Copy
    4
    
    input
    Copy
    9
    1 2 1 3 2 2 2 2 3
    
    output
    Copy
    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.

     

    【题意】

    给你一个数组a,里面有n个整数。你每次可以选择数组中的一个元素ak,从数组中删掉它,再删掉所有值等于ak + 1 或者 ak - 1的元素,这样你可以得到 ak 分。你可以重复进行多次该操作,请问你最后最多能得多少分?

     

    【分析】

    先求出数列中每一个数字k的出现次数num[k]

     

    状态转移方程:如果取得第i-1个数,那么第i-2和第i个数均不可取;反之可取得第i和第i-2个数

     第i个状态的值为 (取得第i-1个数的得分) 与 (取得第i-2个数得分和取得当前数的得分之和) 的最大值

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

    【代码】

    #include<cstdio>
    #include<iostream>
    using namespace std;
    const int N=1e5+5;
    inline int read(){
    	register int x=0;register char ch=getchar();
    	for(;ch<'0'||ch>'9';ch=getchar());
    	for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
    	return x;
    }
    int n,mx;long long cnt[N],f[N];
    int main(){
    	n=read();
    	for(int i=1,x;i<=n;i++) mx=max(mx,x=read()),cnt[x]+=x;
    	f[1]=cnt[1];
    	for(int i=2;i<=mx;i++) f[i]=max(f[i-1],f[i-2]+cnt[i]);
    	printf("%I64d
    ",f[mx]);
    	return 0;
    } 

     

     

     

     

  • 相关阅读:
    递归遍历多维数组(树数据结构)的超级简单方式,并且可以递归超过200层,摘自<<PHP精粹:编写高效PHP代码>>
    http协议传输二进制数据以及对输入流(php://input)和http请求的理解
    一个非常简单的RPC服务
    php://input 打开的数据流只能读取一次,即读取一次之后读取的值为空
    soap的简单实现(PHP)
    使用PHP的curl扩展实现跨域post请求,以及file_get_contents()百度短网址例子
    jquery选取iframe
    算法之棋盘覆盖
    词法分析之实验报告
    简单的词法分析小程序
  • 原文地址:https://www.cnblogs.com/shenben/p/10415251.html
Copyright © 2011-2022 走看看