Antimonotonicity

来源:互联网 发布:心动网络 有员工宿舍么 编辑:程序博客网 时间:2024/05/01 03:18

Antimonotonicity

Time Limits: 1000 ms Memory Limits: 128000 KB

Description
给你1-N的一个排列,数列中的数字互不相等,要求找出最长的子序列a,满足a1 > a2,a2 < a3,a3 > a4,a4 < a5……

Input
T 代表T组数据 T<=50
每组数据一行: n 代表给你n个数,然后就是n个数 N<=30000

Output
T行 每行一个数:
对于每组数据输出最长子序列的长度

Sample Input

45 1 2 3 4 55 5 4 3 2 15 5 1 4 2 35 2 4 1 3 5

Sample Output

1253

解题思路

题目就是让我们求最长的锯齿状序列的长度。

O(n)扫一遍,发现有“峰”就答案增加一。
证明易证。

#include<cstdio>#include<cstring>#define fo(i,x,y) for(int i=x;i<=y;i++)using namespace std;int t,n,a[30001];int Max(int,int,int);void pushMax(int num);int main(){    scanf("%d",&t);    fo(i,1,t)    {        int ans=0;        bool p=1;        scanf("%d",&n);        fo(i,1,n)scanf("%d",&a[i]);        fo(i,1,n-1)if((p && a[i]>a[i+1])||((!p) && a[i]<a[i+1])){p=!p;ans++;}        printf("%d\n",ans+1);    }}
0 0
原创粉丝点击