CodeForces - 1029B.Creating the Contest(最长上升子序列0(n)解法)
思路:這道題無法用平時0(n^2)的解法來求最長上升子序列,會超時,只能用優化的最長上升子序列算法來求
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 101 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 #include <cstdio> #include <iostream> #include <cmath> #include <string> #include <cstring> #include <queue>using namespace std;int n, a[200000+8], dp[200000+8];int main() {scanf("%d", &n);for(int i = 0; i<n; i++)scanf("%d", &a[i]);int ans = 1;//尋求最優的結果dp[1] = 1;//一開始設最長上升子序列的1for(int i = 0; i<n; i++){if(a[i-1]<a[i] && 2*a[i-1] >= a[i] && dp[i-1]+1>dp[i])//判斷后一個數是不是比前一個數大且小于前一個數的2倍,并且最長上升子序列是否還可以增加dp[i] = dp[i-1]+1;else dp[i] = 1;ans = max(ans, dp[i]);//因為最后一個dp不一定是最長上升子序列,所以要讓某個數把最大的結果存起來 }printf("%d\n", ans);return 0; }
?
轉載于:https://www.cnblogs.com/RootVount/p/10478757.html
總結
以上是生活随笔為你收集整理的CodeForces - 1029B.Creating the Contest(最长上升子序列0(n)解法)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bresenham 生成直线
- 下一篇: linux 服务配置