zoukankan      html  css  js  c++  java
  • codeforce1029B B. Creating the Contest(简单dp,简单版单调栈)

    B. Creating the Contest
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    You are given a problemset consisting of nn problems. The difficulty of the ii-th problem is aiai. It is guaranteed that all difficulties are distinct and are given in the increasing order.

    You have to assemble the contest which consists of some problems of the given problemset. In other words, the contest you have to assemble should be a subset of problems (not necessary consecutive) of the given problemset. There is only one condition that should be satisfied: for each problem but the hardest one (the problem with the maximum difficulty) there should be a problem with the difficulty greater than the difficulty of this problem but not greater than twice the difficulty of this problem. In other words, let ai1,ai2,,aipai1,ai2,…,aip be the difficulties of the selected problems in increasing order. Then for each jj from 11 to p1p−1 aij+1aij2aij+1≤aij⋅2 should hold. It means that the contest consisting of only one problem is always valid.

    Among all contests satisfying the condition above you have to assemble one with the maximum number of problems. Your task is to find this number of problems.

    Input

    The first line of the input contains one integer nn (1n21051≤n≤2⋅105) — the number of problems in the problemset.

    The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (1ai1091≤ai≤109) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order.

    Output

    Print a single integer — maximum number of problems in the contest satisfying the condition in the problem statement.

    Examples
    input
    Copy
    10
    1 2 5 6 7 10 21 23 24 49
    output
    Copy
    4
    input
    Copy
    5
    2 10 50 110 250
    output
    Copy
    1
    input
    Copy
    6
    4 7 12 100 150 199
    output
    Copy
    3
    Note

    Description of the first example: there are 1010 valid contests consisting of 11 problem, 1010 valid contests consisting of 22 problems ([1,2],[5,6],[5,7],[5,10],[6,7],[6,10],[7,10],[21,23],[21,24],[23,24][1,2],[5,6],[5,7],[5,10],[6,7],[6,10],[7,10],[21,23],[21,24],[23,24]), 55 valid contests consisting of 33 problems ([5,6,7],[5,6,10],[5,7,10],[6,7,10],[21,23,24][5,6,7],[5,6,10],[5,7,10],[6,7,10],[21,23,24]) and a single valid contest consisting of 44 problems ([5,6,7,10][5,6,7,10]).

    In the second example all the valid contests consist of 11 problem.

    In the third example are two contests consisting of 33 problems: [4,7,12][4,7,12] and [100,150,199][100,150,199].

    已知序列是单调递增的,找最长序列满足 前一项*2>=后一项,用单调栈(单调定义为:前一项*2>=后一项)实现即可。

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<map>
    #include<set>
    #define maxn 110
    #define maxm 10010
    #define inf 0x3f3f3f
    using namespace std;
    stack<int>s;
    int main()
    {
        while(!s.empty())
            s.pop();
        int n;
        scanf("%d",&n);
        int mm=0;
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            int x;
            scanf("%d",&x);
            if(s.empty())
            {
                sum=1;
                s.push(x);
            }
            else
            {
                while(!s.empty ())
                {
                    int y=s.top();
                    if(y*2>=x)//符合条件就放进去
                    {
                        s.push(x);
                        sum++;
                        break;
                    }
                    else//否则把前一项踢了
                    {
                        s.pop();
                        sum--;
                    }
                }
                if(s.empty ())
                {
                    sum=1;
                    s.push(x);
                }
            }
            if(sum>mm)
                mm=sum;
        }
        printf("%d
    ",mm);
        return 0;
    }
                
  • 相关阅读:
    后缀表达式
    Linux中的硬链接和软链接
    C++中const总结
    atexit()函数
    Linux中的0号进程和1号进程
    什么是可重入函数和不可重入函数
    在线(Online)算法
    PHP验证IP地址输入的准确性:数组数值验证
    PHP网页计时工具——SESSION问题
    软件版本号命名规则
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/9548080.html
Copyright © 2011-2022 走看看