码迷,mamicode.com
首页 > 其他好文 > 详细

BestCoder Round #13 1003(单调性DP)HDU5064

时间:2014-10-12 10:49:17      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:acm   bestcoder   

Find Sequence

Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 68    Accepted Submission(s): 12


Problem Description
Give you an positive integer sequence a1,a2,,ai,,an, and they satisfy a1+a2++ai++an=M(0<M222).
We can find new sequence b1=aid1,b2=aid2,,bx=aidx,,by=aidy,,bt=aidt, where if x != y then idx!=idy. and this sequence satisfy:
(1) b1b2bt 
(2) b2?b1b3?b2?bt?bt?1
We can find many sequences b1,b2,b3,,bt. But we only want to know maximum t.
 

Input
The first line in the input file is an Integer T(1T30).
The first line of each test case contains two integer n,M(0<M222).
Then a line have n integer, they represent a1,a2,,ai,,an.
 

Output
For each test case, output the maximum t.
 

Sample Input
2 6 19 3 2 1 3 4 6 1 4194304 4194304
 

Sample Output
5 1
Hint
For the first testcase, The Sequence is 1 2 3 4 6

题意:RT

思路:首先排序是肯定的,然后无论怎么选条件1始终满足

            现在看条件2,因为条件2还跟前后的差值有关

            那么就要考虑DP了
       
            用DP[i][j]表示以第i和第j个数结尾的最长子序列

            那么转移方程为DP[i][j]=max(DP[k][i]) + 1,其中j>i ,i>=k

            这样暴力算是n^3的复杂度,显然会超时

            注意这是有个单调性的,DP[i][j]满足的最优值一定DP[i][j+1]也可以取,所以在j增大的时候,k不必重新从i开始递减(因为这段重复的区间之前已经算过了)

            k只需要继续以前的位置递减即可,保存最优值,然后赋给dp[i][j]

BestCoder Round #13 1003(单调性DP)HDU5064

标签:acm   bestcoder   

原文地址:http://blog.csdn.net/cq_phqg/article/details/40006161

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!