• [DP]CF455A Boredom


    A. Boredom

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

    Output

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

    正确解法:

    刚开始以为是一个区间dp,以为是把a[k]删去,a[k+1]和a[k-1]的数也要删去。

    但是开的区间数组1e5开不下,去看题解,以为有更好的方法。

    于是……题目看错了

    是把某个数a删去,a+1和a-1也要删去。

    我们设f[i]为最大值为i的最大价值。

    若i不被删的话,f[i]=f[i-1]

    若把i删除,f[i]=f[i-2]+cnt[i]*i;   

    (i删除后,i-1就不能选择,就要从f[i-2]开始转移)

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<algorithm>
     4 #include<iostream>
     5 #include<string>
     6 #include<map>
     7 #include<set>
     8 #include<vector>
     9 #include<queue>
    10 #include<cmath>
    11 #include<cstdlib>
    12 #include<stack>
    13 #define de printf("
    debug:
    ")
    14 #define End printf("
    end
    
    ")
    15 #define fi first
    16 #define se second
    17 #define P pair< int, int >
    18 #define PII pair< pair<int, int> ,int>
    19 #define INF 0x3f3f3f3f
    20 using namespace std;
    21 typedef long long ll;
    22 const ll mod=1000000007;
    23 const int N=100000+100;
    24 const int inf=0x7fffffff;
    25 int n,a[N],maxx;
    26 ll cnt[N];
    27 ll f[N];
    28 int main()
    29 {
    30     scanf("%d",&n);
    31     for(int i=1;i<=n;i++)
    32     {
    33         scanf("%d",&a[i]);
    34         maxx=max(maxx,a[i]);
    35         cnt[a[i]]++;
    36     }
    37     f[0]=0;
    38     f[1]=cnt[1];
    39     for(int i=2;i<=maxx;i++)
    40         f[i]=max(f[i-1],f[i-2]+cnt[i]*i);
    41     printf("%lld
    ",f[maxx]);
    42 
    43 
    44 
    45     return 0;
    46 }
    View Code
  • 相关阅读:
    记一次RAID阵列的迁移经历
    PAT A1131 Subway Map
    PAT A1103 Integer Factorization
    ZOJ1002 Fire Net
    L3022 地铁一日游
    github的使用小技巧
    hexo博客部署在阿里云上
    Windows环境搭建hexo博客部署到GitHub上
    Centos7.X下安装node.js npm和 淘宝镜像(cnpm)
    今天新注册的,第一篇文章
  • 原文地址:https://www.cnblogs.com/Kaike/p/11260597.html
Copyright © 2020-2023  润新知