(DP) poj 3298

Antimonotonicity

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 2913   Accepted: 1266

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

拐点数+1

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<algorithm>
#include<cstdlib>
using namespace std;
int tt,a[30005];
int main()
{
    int n,x1,x2,k;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d",&n);
        int ans=1;
        scanf("%d",&x1);
        k=2;
        for(int i=2;i<=n;i++)
        {
            scanf("%d",&x2);
            if(x1>x2&&k!=1)
            {
                k=1;
                ans++;
            }
            if(x1<x2&&k!=2)
            {
                k=2;
                ans++;
            }
            x1=x2;
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-10-09 18:22:33

(DP) poj 3298的相关文章

poj 3298 Antimonotonicity 贪心

题意: 求一个序列的最大子序列,该子序列满足:a1>a2<a3>a4...... 分析: 贪心,从极大值起交替取这个序列中极小值.极大值. 代码: //poj 3298 //sep9 #include <iostream> using namespace std; const int maxN=30024; int a[maxN]; int main() { int cases; scanf("%d",&cases); while(cases--)

[dp] poj 1015 Jury Compromise

题目链接: http://poj.org/problem?id=1015 Jury Compromise Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24438   Accepted: 6352   Special Judge Description In Frobnia, a far-away country, the verdicts in court trials are determined by a jury

[kuangbin 基础dp][POJ 1015] Jury Compromise(dp)

[kuangbin 基础dp][POJ 1015] Jury Compromise 题目 In Frobnia, a far-away country, the verdicts in court trials are determined by a jury consisting of members of the general public. Every time a trial is set to begin, a jury has to be selected, which is do

(状态压缩DP) poj 2441

Arrange the Bulls Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 3709   Accepted: 1422 Description Farmer Johnson's Bulls love playing basketball very much. But none of them would like to play basketball with the other bulls because the

树形dp poj 2342

题目链接:poj 2342 题目大意:某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知每个人的活跃指数和上司关系(当然不可能存在环),求邀请哪些人(多少人)来能使得晚会的总活跃指数最大. 思路:既然是树形dp,自然先建立一颗树了,这里用的是邻接表(L<-K).找根的时候利用flag数组来标记所有儿子节点,那么没有标记的自然是根节点了.树形dp从叶子节点开始dp,所以深搜到叶子节点,然后不断回溯给父节点.在处理儿子节点时,父节点不参加

递推DP POJ 1163 The Triangle

题目传送门 1 /* 2 数塔 3 自底向上 4 */ 5 #include <cstdio> 6 #include <iostream> 7 #include <cstring> 8 #include <string> 9 #include <algorithm> 10 #include <cmath> 11 using namespace std; 12 13 const int MAXN = 100 + 10; 14 const

状态压缩DP——POJ 2923

对应POJ题目:点击打开链接 Exponentiation Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 2923 Description Emma and Eric are moving to their new house they bought after returning from their honeymoon. Fortu

POJ 3298 Antimonotonicity (思维)

题目链接:http://poj.org/problem?id=3298 找一个最长不要求连续的子序列,如a1 > a3 < a6 > a7 ... 举个例子模拟一下差不多明白了,a[i - 1]与a[i]有依赖关系. 1 //#pragma comment(linker, "/STACK:102400000, 102400000") 2 #include <algorithm> 3 #include <iostream> 4 #include

(拓扑图+DP) poj 3249

Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 9563   Accepted: 2201 Description Mr.Dog was fired by his company. In order to support his family, he must find a new job as soon as possible. Nowadays, It's hard to have a job