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=;
int a[maxn],b[maxn],f[maxn][maxn],cnt,ans;
int main()
{
int T,N,pos,i,j;
scanf("%d",&T);
while(T--){
ans=; scanf("%d",&N);
for(i=;i<=N;i++) scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b+N+);
cnt=unique(b+,b+N+)-(b+);
memset(f,,sizeof(f));
for(i=;i<=N;i++){
pos=lower_bound(b+,b+cnt+,a[i])-b;
for(j=i;j>=;j--) if(f[pos][j-]) f[pos][j]=max(f[pos][j],f[pos][j-]+j);
f[pos][]=ans+;
for(j=;j<=i;j++) ans=max(ans,f[pos][j]);
}
printf("%d\n",ans);
}
return ;
}

SPOJ:Collecting Candies(不错的DP)的更多相关文章

  1. SPOJ:Harbinger vs Sciencepal(分配问题&不错的DP&bitset优化)

    Rainbow 6 is a very popular game in colleges. There are 2 teams, each having some members and the 2 ...

  2. spoj 1812 LCS2(SAM+DP)

    [题目链接] http://www.spoj.com/problems/LCS2/en/ [题意] 求若干个串的最长公共子串. [思路] SAM+DP 先拿个串建个SAM,然后用后面的串匹配,每次将所 ...

  3. poj2096 Collecting Bugs(概率dp)

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 1792   Accepted: 832 C ...

  4. poj 2096 Collecting Bugs(期望 dp 概率 推导 分类讨论)

    Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other ...

  5. Collecting Bugs poj2096 概率DP

                                                                Collecting Bugs Time Limit: 10000MS   Me ...

  6. SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]

    题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...

  7. poj 2096 Collecting Bugs 【概率DP】【逆向递推求期望】

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 3523   Accepted: 1740 ...

  8. 【POJ 2096】Collecting Bugs 概率期望dp

    题意 有s个系统,n种bug,小明每天找出一个bug,可能是任意一个系统的,可能是任意一种bug,即是某一系统的bug概率是1/s,是某一种bug概率是1/n. 求他找到s个系统的bug,n种bug, ...

  9. SPOJ 1435 Vertex Cover 树形DP

    i 表示节点 i ,j=0表示不选择其父节点,j=1表示选择其父节点.f 为其父节点. 取 每个节点选择/不选择 两者中较小的那个. 一组数据: 151 21 31 41 1010 910 1112 ...

随机推荐

  1. python练习之-计算器

    学习以堆栈模式编写-计算器 堆栈特点:先进后出, 如下: #!/opt/python3/bin/python3 # Author: yong import re def is_symbol(eleme ...

  2. Perl、PHP、Python、Java和Ruby的比较

    提问 ◆ Perl.Python.Ruby和PHP各自有何特点? ◆ 为什么动态语言多作为轻量级的解决方案? ◆ LAMP为什么受欢迎? ◆ Ruby on Rails为什么会流行? ◆ 编程语言的发 ...

  3. flask如何处理并发

    1.使用自身服务器的多进程或者多线程,参考werkzeug的run_simple函数的入参.注意,进程和线程不能同时开启 2.使用gunicorn使用多进程,-w worker 进程数,类型于运行多个 ...

  4. OHIFViewer meteor build 问题

    D:\Viewers-master\OHIFViewer>meteor build --directory d:/h2zViewerC:\Users\h2z\AppData\Local\.met ...

  5. 【转载】容器技术 & Docker & 与虚拟化的比较

    看到10月份天天写博客,只有一天没写,非常棒! 11月份也基本每天都写,现在看到有三天没加新博客,应该是之前挖的坑太多了,需要填坑,呵呵. 那这篇文章是不是为了占坑呢?哈哈.我不说话. 容器技术,这篇 ...

  6. Tomcat载入两次问题

    前提介绍: 1.某个应用Dragon放置路径:/opt/apache-tomcat-7.0.47/webapps/Dragon 2.Tomcat的server.xml部分配置信息例如以下:    &l ...

  7. poj 1694 An Old Stone Game 树形dp

    //poj 1694 //sep9 #include <iostream> #include <algorithm> using namespace std; const in ...

  8. BZOJ 2809 APIO 2012 dispatching 平衡树启示式合并

    题目大意:给出一棵树,每个节点有两个值,各自是这个忍者的薪水和忍者的领导力.客户的惬意程度是这个点的领导力乘可以取得人数.前提是取的人的薪水总和不超过总的钱数. 思路:仅仅能在子树中操作.贪心的想,我 ...

  9. 使用maven创建项目和cannot change version web module 3.0

    近期下载了最新的Eclipse mars.2, 这个eclipse自带了maven插件,于是就用maven尝试创建一个java web项目. 第一步,例如以下图所看到的选择 Maven Project ...

  10. HTML URL编码规则

    将空格转换为加号(+) 对0-9,a-z,A-Z之间的字符保持不变 对于所有其他的字符,用这个字符的当前字符集编码在内存中的十六进制格式表示,并在每个字节前加上一个百分号(%).如字符“+”用%2B表 ...