POJ 3298 递推,DP

原创 2011年01月14日 23:55:00
Antimonotonicity
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2428   Accepted: 995

Description

I have a sequence Fred of length n comprised of integers between 1 and n inclusive. The elements of Fred are pairwise distinct. I want to find a subsequence Mary of Fred that is as long as possible and has the property that:

Mary0 > Mary1 < Mary2 > Mary3 < ...

Input

The first line of input will contain a single integer T expressed in decimal with no leading zeroes. T will be at most 50. T test cases will follow.

Each test case is contained on a single line. A line describing a test case is formatted as follows:

n Fred0 Fred1 Fred2 ... Fredn-1.

where n and each element of Fred is an integer expressed in decimal with no leading zeroes. No line will have leading or trailing whitespace, and two adjacent integers on the same line will be separated by a single space. n will be at most 30000.

Output

For each test case, output a single integer followed by a newline --- the length of the longest subsequence Mary of Fred with the desired properties.

Sample Input

4
5 1 2 3 4 5
5 5 4 3 2 1
5 5 1 4 2 3
5 2 4 1 3 5

Sample Output

1
2
5
3

Source

利用凹凸函数,依次递推。。

Source Code

Problem: 3298   User: bingshen
Memory: 252K   Time: 172MS
Language: C++   Result: Accepted
  • Source Code
    #include<stdio.h>
    #include<string.h>
    
    int a[30005];
    
    int main()
    {
    	int i,t,n,m,ans;
    	bool s;
    	scanf("%d",&t);
    	while(t--)
    	{
    		ans=1;
    		scanf("%d",&n);
    		for(i=1;i<=n;i++)
    			scanf("%d",&a[i]);
    		m=a[1];
    		s=true;
    		for(i=2;i<=n;i++)
    		{
    			if(m<a[i]&&s)
    				m=a[i];
    			else if(m>a[i]&&!s)
    				m=a[i];
    			else if(m>a[i]&&s)
    			{
    				m=a[i];
    				s=false;
    				ans++;
    			}
    			else if(m<a[i]&&!s)
    			{
    				m=a[i];
    				s=true;
    				ans++;
    			}
    		}
    		printf("%d/n",ans);
    	}
    	return 0;
    }
    

相关文章推荐

POJ3420 递推/状态压缩DP +矩阵幂加速处理

POJ3420 Quad Tiling 现在我们有一个4*n(1 输入:包含多组实例,每组为n和m(0 输出:输出摆放总数对m求余的结果。 分析:做本题首先要知道它的基础题型。见:http:/...

【POJ 2663】Tri Tiling(dp|递推)

不求独避风雨外,只笑桃源非梦中

POJ 2229 Sumsets(dp 递推)

Farmer John commanded his cows to search for different sets of numbers that sum to a given number. T...

POj 3420 Quad Tiling 状态压缩DP+递推+矩阵快速幂

哈哈,写了好久的,总算对了。 先介绍一种   递推+矩阵的快速幂的方法   一种DP的思想考虑4×n的最后一列  ,可以放的方法一共有5种    1.放4个 1×2  则 为dp[n-2]   2....

【POJ 3420】Quad Tiling(dp|递推 +矩阵快速幂)

秋意渐浓,阳光西斜,遍地金黄

poj 2506 Tiling dp 递推

我是有用dp做的,做完之后看网上大多人都是推出公式,反正我是没看出来 dp[i][j],i表示第i列放入一竖条,j表示放入的竖条是什么形状,j一共有5种情况,dp就表示这个i表示第i列放入一个j类型...

POJ 1163 The Triangle【DP】递归和递推

7 3 8 8 1 0 2 7 4 4 4 5 2 6 5(Figure 1)Figure 1 shows a number triangle. Write a...
  • SCaryon
  • SCaryon
  • 2017年03月03日 22:11
  • 113

POJ3267 The Cow Lexicon ACM解题报告(dp递推)

萌萌哒的初学者poj终于做满100题了,可惜这题不是靠自己的能力过的,因为看到这个单词串,实在是想不出状态转移方程呢。 参考了下百度还是很快理解的,从字符串的尾巴开始递推,字符串是0-L-1,设d[...

POJ1088 滑雪 经典DP 记忆化搜索和递推

POJ1088 滑雪 经典DP 记忆化搜索和递推

[状态压缩DP/递推/位运算] Pku/Poj Corn Fields 状态DP入门题详细注释。

Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12)...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:POJ 3298 递推,DP
举报原因:
原因补充:

(最多只允许输入30个字)