hrbust 1131 波动序列【水题】

来源:互联网 发布:小型机房网络拓扑 编辑:程序博客网 时间:2024/04/27 19:28

波动序列Time Limit: 1000 MSMemory Limit: 65536 KTotal Submit: 359(95 users)Total Accepted: 111(83 users)Rating: Special Judge: NoDescription

元帅对同学们的做题情况有了大致的了解,大家参差不齐,做题数目很难保证,元帅情绪很波动,脑海中有一串波动序列,有一个长度为N的整数序列,序列里面的数是两两不同的,现在要在里面找一个波动序列,这个序列越长越好。

比如有波动序列{a0,a1,a2…an},则a0 > a1 < a2 > a3 < …

Input

第一行输入一个数T,代表有T个任务,T不大于50。

对于每个任务,输入格式为

N a0 a1 a2 … aN

其中N<=30000,测试数据保证序列的数两两不同。

Output

对于每个任务,输出最长的波动序列长度

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
Author王勇

思路:

按照贪心的思路去走,先找到递增的递增顶点,然后再找递减的递减顶点。换句话说就是递增找到不能增为止,递减找到不能减为止,那么这个顶点,就是属于波动序列中的点。

解题方式还是蛮巧妙的哈!

Ac代码:


#include<stdio.h>#include<string.h>using namespace std;int a[300000];int main(){    int t;    scanf("%d",&t);    while(t--)    {        int n;        scanf("%d",&n);        for(int i=0;i<n;i++)        {            scanf("%d",&a[i]);        }        int i=1;        int d=1;        int output=0;        while(i<n)        {            if(d==1)            {                while(i<n)                {                    if(a[i]>=a[i-1])i++;                    else break;                }                output++;                d=-1;            }            else            {                while(i<n)                {                    if(a[i]<=a[i-1])i++;                    else break;                }                output++;                d=1;            }            //printf("%d\n",i);        }        printf("%d\n",output);    }}










0 0
原创粉丝点击