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

     

     

     

     

  • 相关阅读:
    js上传Excel文件
    在typescript中import第三方类库clipboard报错
    webpack学习(一)安装和命令行、一次js/css的打包体验及不同版本错误
    querySelector和getElementById之间的区别
    关于js延迟加载(异步操作)的方式
    addEventListener与attachEvent
    ES6 的Object.assign(target, source_1, ..., source_n)方法与对象的扩展运算符
    JS实现生成一个周对应日期数组
    微信小程序之媒体查询@media
    微信小程序之页面引用utils中的js文件
  • 原文地址:https://www.cnblogs.com/shenben/p/10415251.html
Copyright © 2011-2022 走看看