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 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.
Input
The 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.
Output
Print 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
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]), 55valid 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].
直接暴力(超水),代码如下:
#include<bits/stdc++.h>
#define closeio ios::sync_with_stdio(0),cin.tie(0)
using namespace std;
const int maxn=2e5+100;
int arr[maxn];
int main()
{
closeio;
int n;
cin>>n;
int lcount=1;
int ans=1;
cin>>arr[0];
for(int i=1;i<n;i++)
{
cin>>arr[i];
if(arr[i]<=2*arr[i-1])
{
lcount++;
ans=max(ans,lcount);
}
else
lcount=1;
}
cout<<ans<<endl;
}
这道题可以尺取法,代码如下:
#include<bits/stdc++.h>
#define closeio ios::sync_with_stdio(0),cin.tie(0)
const int maxn=2e5+100;
int a[maxn];
using namespace std;
int main()
{
closeio;
int n,ans=1,l,r;
cin>>n;
cin>>a[0];
l=a[0];
int j=0;///j是区间最左边的,i是区间最右边的,l是i左边的(用来判断条件是否满足);
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<<endl;
}
还有一种算是动态规划的解法
代码如下:
#include<bits/stdc++.h>
#define closeio ios::sync_with_stdio(0),cin.tie(0)
const int maxn=2e5+100;
using namespace std;
int dp[maxn];
int a[maxn];
int main()
{
closeio;
int result=1;
int n;
cin>>n;
dp[0]=1;
cin>>a[0];
for(int i=1;i<n;i++)
{
dp[i]=1;
cin>>a[i];
if(a[i]<=2*a[i-1])
dp[i]=dp[i-1]+1;
result=max(result,dp[i]);
}
cout<<result<<endl;
}