2020-2-2 摸底测试D

题目大意:

给定一个上升序列a,求一个子序列b,要求子序列中任意bi<=bi-1*2,也就是说,前一个数的2倍要大于等于后一个数,求这样的子序列的最大长度。

看了别人的博客,思路如下:

这里有一个很重要的性质:如果当前元素不能添加到当前序列中,那么它之后的必定也不能添加进去。也就是说,当前序列到此为止。也就是说,求出的子序列必定连续。

那么只要用一个计数器记录当前序列长度就可以了,如果能放进去就+1,否则用这个更新答案,然后置为1开始新一轮计数。

#include <cstdio>
int max(int a,int b){return a>b?a:b;}
int ans,temp=1047483645,cnt,N;
int main(){
    scanf("%d",&N);
    for(int i=1;i<=N;i++){
        int x;
        scanf("%d",&x);
        if(x<=2*temp)cnt++;
        else cnt=1;
        ans=max(ans,cnt);
        temp=x;
    }
    printf("%d",ans);
    return 0;
}

 

posted @ 2020-02-03 14:03  工程1  阅读(146)  评论(0)    收藏  举报