zoukankan      html  css  js  c++  java
  • SPOJ:Collecting Candies(不错的DP)

    Jonathan Irvin Gunawan is a very handsome living person. You have to admit it to live in this world.

    To become more handsome, Jonathan the Handsome have to collect candies (no relation, indeed). In front of him, there are N candies with different level of sweetness. Jonathan will collect the candies one by one. Jonathan can collect any number of candies, but he must collect the candy in the increasing order of level of sweetness (no two candies will have the same level of sweetness).

    Every candy has their own color, which will be represented by a single integer between 0 and 109 inclusive.

    If Jonathan collects the first candy, or a candy that has different color with the previous candy he take, he will get 1 point.

    If Jonathan collects the candy that has the same color with the previous candy, he will get a combo. Combo-x means that he has collected x candies of the same color consecutively. In other words, if he collect a candy and get combo-(x-1) and he collect a candy with the same color again, he will get combo-(x). And then if he collects a candy with different color, the combo will vanish and back to combo- 1.

    (Note : previous candy means the last candy he take)

    Every time he get combo-x, he will get x points. Jonathan wants to count how many maximum total points he can get. You are a fan of Jonathan the Handsome have to help him.

    Input

    The first line consists of a single integer T, indicating the number of
    testcases.
    For every testcase, the first line consists of a single integer N.
    The next line consists of N integers, representing the color of the candy given in the increasing level of sweetness, separated by a single space.

    The first line consists of a single integer T, indicating the number of testcases.

    For every testcase, the first line consists of a single integer N (1 ≤ N ≤ 1000).

    The next line consists of N integers, representing the color of the candy given in the increasing level of sweetness, separated by a single space.

    Output

    For every case, output a single integer consist of the maximum total points Jonathan can get.

    Example

    Input:
    2
    4 
    1 1 2 1 
    4 
    1 2 3 1
    
    Output:
    6
    4
    

    Explanation

    In the first sample, Jonathan chooses not to take the candy with color 2, as he will lose the combo. In the second sample, he will get a maximum total points if he take all of the candies.

    题意:N个数,取其子数列,使得总得分最高。得分定义如下:

           对于某一个x

           若前面有连续的c个x,则得分为c + 1

           e.g.我选择下面这些数。

         1 2 1 1 3 3 3 3 2 2 1 (取到的数列)

         1 1 1 2 1 2 3 4 1 2 1 (得分分值)

    思路:DP,先离散化。

          状态F[i, j] ,i ——取到的最后一个数为i (i是离散化化后的数字),j ——前面有连续j个i ,F[i, j] ——这种情况下的最大得分

          对于当前的数now

               F[now, j + 1] = F[now, j] + j (1’)

               F[now, 1] = max{F[i, j]} + 1 (i != now) (2’)

     (感悟:平时的DP,即便是二维的DP,其状态都是一维的。所以想到还是有点难想到。读者有兴趣可以自己想试着想一下。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1010;
    int a[maxn],b[maxn],f[maxn][maxn],cnt,ans;
    int main()
    {
        int T,N,pos,i,j;
        scanf("%d",&T);
        while(T--){
            ans=0; scanf("%d",&N);
            for(i=1;i<=N;i++) scanf("%d",&a[i]),b[i]=a[i];
            sort(b+1,b+N+1);
            cnt=unique(b+1,b+N+1)-(b+1);
            memset(f,0,sizeof(f));
            for(i=1;i<=N;i++){
                pos=lower_bound(b+1,b+cnt+1,a[i])-b;    
                for(j=i;j>=2;j--) if(f[pos][j-1]) f[pos][j]=max(f[pos][j],f[pos][j-1]+j);
                f[pos][1]=ans+1;
                for(j=1;j<=i;j++) ans=max(ans,f[pos][j]);
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    分享50款 Android 移动应用程序图标【上篇】
    Matter.js – 你不能错过的 2D 物理引擎
    Materialize
    Gulp.js 参考手册,自动化构建利器
    严谨而浪漫!20个来自德国网页设计
    Muzli – 所有你需要的设计灵感都在这
    FormatJS – 让你的 Web 应用程序国际化
    字体大宝库:20款免费的情人节字体
    15个优秀的 Material Design(材料设计)案例
    SpaceBase – 基于 Sass 的响应式 CSS 框架
  • 原文地址:https://www.cnblogs.com/hua-dong/p/9083367.html
Copyright © 2011-2022 走看看