CodeForces - 1029B(暴力/LIS/尺取)

Creating the Contest
You are given a problemset consisting of nnproblems. 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 p−1p−1 aij+1≤aij⋅2aij+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.InputThe first line of the input contains one integer nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of problems in the problemset.The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an(1≤ai≤1091≤ai≤109) — difficulties of the problems. It is guaranteed that difficulties of the problems are distinct and are given in the increasing order.OutputPrint a single integer — maximum number of problems in the contest satisfying the condition in the problem statement.
Examples
Input
10
1 2 5 6 7 10 21 23 24 49
Output
4
Input
5
2 10 50 110 250
Output
1
Input
6
4 7 12 100 150 199
Output
3
NoteDescription of the first example: there are 1010 valid ctests consisting of 11 problem, 1010 alid contests consisting of 22 roblems ([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 id 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]

求满足题意的最长子序列的长度
简化LIS:

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
long long a[200050],dp[200050],i,j,n,ans;
int main()
{
   
    while(scanf("%lld",&n)!=EOF)
    {
   
        ans=0;
        for(i=0;i<n;i++)
        {
   
            scanf("%lld",&a[i]);
            dp[i]=1;
            if(i&&a[i]<=2*a[i-1]&&a[i]>a[i-1])
                dp[i]=dp[i-1]+1;
            ans=max(ans,dp[i]);
        }
        cout<<ans<<'\n';
     }
     return0;
 }

暴力扫一遍:

#include <cstdio>
#include <iostream>
using namespace std;
const int N=2e5+40;
int a[N];
int main()
{
   
    int n,i,ans,m;
    while(scanf("%d",&n)!=EOF)
    {
   
        ans=m=1;
        for(i=0;i<n;i++)
        {
   
            scanf("%d",&a[i]);
            if(i&&a[i]<=2*a[i-1])
            {
   
                m++;
                ans=max(ans,m);
            }
            else if(i&&a[i]>2*a[i-1])
                m=1;
        }
        cout<<ans<<'\n';
    }
    return 0;
}

尺取法:对数组保存下标 高效。

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+40;
int a[N];
int main()
{
   
    int ans,n,l;
    while(scanf("%d",&n)!=EOF)
    {
   
        ans=1;
        cin>>a[0];
        l=a[0];
        int j=0;
        for(int i=1;i<n;i++)
        {
   
            cin>>a[i];
            if(a[i]<=2*l)
                ans=max(ans,i-j+1);
            else
                j=i;
            l=a[i];
        }
        cout<<ans<<'\n';
    }
    return 0;
}
全部评论

相关推荐

uu们,拒offer时hr很生气怎么办我哭死
爱睡觉的冰箱哥:人家回收你的offer,或者oc后没给你发offer的时候可不会愧疚你,所以你拒了也没必要愧疚他。
点赞 评论 收藏
分享
Southyeung:我说一下我的看法(有冒犯实属抱歉):(1)简历不太美观,给我一种看都不想看的感觉,感觉字体还是排版问题;(2)numpy就一个基础包,机器学习算法是什么鬼?我感觉你把svm那些写上去都要好一点。(2)课程不要写,没人看,换成获奖经历;(3)项目太少了,至少2-3个,是在不行把网上学习的也写上去。
点赞 评论 收藏
分享
一表renzha:手写数字识别就是一个作业而已
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务