【題解】codeforces1029B[Codeforces Round #506 (Div. 3)]B.Creating the Contest 貪心
Description
You are given a problemset consisting of problems. The difficulty of the -th problem is . 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 be the difficulties of the selected problems in increasing order. Then for each from to 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 — the number of problems in the problemset.
The second line of the input contains n integers — 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 valid contests consisting of problem, valid contests consisting of problems , valid contests consisting of problems and a single valid contest consisting of problems .
In the second example all the valid contests consist of problem.
In the third example are two contests consisting of problems: and .
最初把題目看錯了……交了兩遍反應過來。就只需要貪心地把可加入的合併到一段裡去,不能加入的開新的一段。
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=2e5+10;
int a[N],ans,n;
int main()
{
//freopen("in.txt","r",stdin);
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
for(int i=0;i<n;i++)
{
int pos=i;
while(pos+1<n&&a[pos+1]<=a[pos]*2)pos++;
ans=max(ans,pos-i+1);
i=pos;
}
printf("%d\n",ans);
return 0;
}
總結
仔細審題很重要