Antimonotonicity

来源:互联网 发布:sql基础教程 第二版 编辑:程序博客网 时间:2024/04/30 22:51

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

The solution

乍一眼看过去,有点亲切,最长不下降子序列?差不多。。。dp?有搞头。。。
于是乎边埋头开打。。。结果被题解逗了。。(*  ̄︿ ̄)

题解很简单,设一个last,记录之前取得数,因为题目要求的是像山峰似得数列,大概像
这样吧:“W”,汗。。,于是可以把下降的标记为true,把上升的标记为false,之后再yy

具体如下

如果现在取了奇数个(即为要下降的),即要取的小于之前最后取的,若目前的数a[i]>last,则将last用a[i]替换,否则取a[i];
如果现在取了偶数个(即为要上升的),即要取的大于之前最后取的,若目前的数a[i]

code

#include <cstdio>#include <iostream>#include <cmath>#include <cstring>#include <algorithm>#define fo(i,a,b) for (int i=a;i<=b;i++)#define fd(i,a,b) for (int i=a;i>=b;i--)#define N 30005using namespace std;int main(){    int T,n,x;    scanf("%d",&T);    while (T--)    {        scanf("%d",&n);        scanf("%d",&x);        int last=x,ans=1;        bool mark=false;        fo(i,2,n)         {            scanf("%d",&x);            if ((!mark && x<=last) || (mark && x>=last)) ans++,mark=!mark;            last=x;        }        printf("%d\n",ans);    }}
0 0