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 (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
题意: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)的更多相关文章
- 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 ...
- spoj 1812 LCS2(SAM+DP)
[题目链接] http://www.spoj.com/problems/LCS2/en/ [题意] 求若干个串的最长公共子串. [思路] SAM+DP 先拿个串建个SAM,然后用后面的串匹配,每次将所 ...
- poj2096 Collecting Bugs(概率dp)
Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 1792 Accepted: 832 C ...
- poj 2096 Collecting Bugs(期望 dp 概率 推导 分类讨论)
Description Ivan is fond of collecting. Unlike other people who collect post stamps, coins or other ...
- Collecting Bugs poj2096 概率DP
Collecting Bugs Time Limit: 10000MS Me ...
- SPOJ BALNUM - Balanced Numbers - [数位DP][状态压缩]
题目链接:http://www.spoj.com/problems/BALNUM/en/ Time limit: 0.123s Source limit: 50000B Memory limit: 1 ...
- poj 2096 Collecting Bugs 【概率DP】【逆向递推求期望】
Collecting Bugs Time Limit: 10000MS Memory Limit: 64000K Total Submissions: 3523 Accepted: 1740 ...
- 【POJ 2096】Collecting Bugs 概率期望dp
题意 有s个系统,n种bug,小明每天找出一个bug,可能是任意一个系统的,可能是任意一种bug,即是某一系统的bug概率是1/s,是某一种bug概率是1/n. 求他找到s个系统的bug,n种bug, ...
- SPOJ 1435 Vertex Cover 树形DP
i 表示节点 i ,j=0表示不选择其父节点,j=1表示选择其父节点.f 为其父节点. 取 每个节点选择/不选择 两者中较小的那个. 一组数据: 151 21 31 41 1010 910 1112 ...
随机推荐
- 基于CI框架的管理系统
1:ci框架是有入口文件的,前端和后台入口文件(index.php,admin.php):里面修改$application_folder = 'application/home': 2:项目基本都是在 ...
- 一个强大的Android模拟器Genymotion
相信很多Android开发者一定受够了速度慢.体验差效率及其地下的官方模拟器了,自己在平时的开发中几乎是不会用模拟器的,等的时间太久了,但是在一些尺寸适配或是兼容性测试的时候没有足够多的机器进行测试, ...
- 洛谷 P3865 【模板】ST表
P3865 [模板]ST表 题目背景 这是一道ST表经典题——静态区间最大值 请注意最大数据时限只有0.8s,数据强度不低,请务必保证你的每次查询复杂度为 O(1)O(1) 题目描述 给定一个长度为 ...
- IntelliJ IDEA删除项目
删除项目一向比较奇葩,因为当你点击到该项目名称右键时,并没有delete选项,导致我们不知道怎么删除,查找多方文档,得到以下解决: 1.将鼠标移到要删除的项目名称上,单击并按“Delete”按钮删除项 ...
- 解决Linux系统没有/etc/sysconfig/iptables文件
Linux系统中,防火墙默认是不开启的,一般也没有配置过任何防火墙的策略,所以不存在/etc/sysconfig/iptables文件. 一.常规解决方法: 1.在控制台使用iptables命令随便写 ...
- BUPT复试专题—旋转图像(2014)
题目描述 将一幅只含有01像素点的图片进行顺时针旋转,旋转的角度仅包含0°,90°,180°,270° 输入 第一行一个整数T(<50)表示输入的组数 每组测试数据第一行是两个整数N和M(< ...
- Android 系统广播机制
一.Android应用程序注冊广播接收器(registerReceiver)的过程分析 參考Android应用程序注冊广播接收器(registerReceiver)的过程分析http://blog.c ...
- 怎样提高hbase的入库性能
hbase写数据首先先写入memstore.当memstore满64MB以后,会flush到disk上而成为storefile.当storefile数量超过3时,会启动compaction过程将它们合 ...
- AAuto如何发布EXE文件
1 如下图所示,谷歌翻译是AAuto提供的源码,我们现在把它做成软件.点击编译,注意看底部状态栏提示,编译之后的谷歌翻译还是aau格式的,双击可以直接运行.但是体积变大了,而且已经是二进制文件,无法再 ...
- MaterialImageLoading
https://github.com/eltld/MaterialImageLoading