当前位置:网站首页>AcWing 4489. 最长子序列
AcWing 4489. 最长子序列
2022-07-03 17:16:00 【永远有多远.】
给定一个长度为 nn 的严格单调递增的整数序列 a1,a2,…,an。
序列 aa 的子序列可以表示为 ai1,ai2,…,aipai1,ai2,…,aip,其中 1≤i1<i2<…<ip≤n1≤i1<i2<…<ip≤n。
我们希望找到一个 aa 的子序列,使得该子序列满足:对于 j∈[1,p−1]j∈[1,p−1],aij+1≤aij×2恒成立。
我们认为,长度为 11 的子序列总是满足条件的。
请你计算并输出满足条件的子序列的最大可能长度。
输入格式
第一行包含一个整数 n。
第二行包含 n 个整数 a1,a2,…,an。
输出格式
一个整数,表示满足条件的子序列的最大可能长度。
数据范围
前 5 个测试点满足 1≤n≤10。
所有测试点满足 1≤n≤2×10^5,1≤a1<a2<…<an≤10^9。
输入样例1:
10
1 2 5 6 7 10 21 23 24 49
输出样例1:
4
输入样例2:
5
2 10 50 110 250
输出样例2:
1
输入样例3:
6
4 7 12 100 150 199
输出样例3:
3思路1:贪心
AC代码:
#include <iostream>
using namespace std;
const int MAXN = 2e5 + 7;
int arr[MAXN], n, ans = 1, cnt = 1;
int main() {
cin.tie(nullptr)->ios::sync_with_stdio(false);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> arr[i];
}
for (int i = 1; i <= n - 1; i++) {
if (arr[i + 1] <= arr[i] * 2) {
cnt++;
} else {
ans = max(ans, cnt);
cnt = 1;
}
}
//注意最后ans为max(ans, cnt) 因为最后一次更新的cnt还未与ans进行比较
cout << max(ans, cnt) << endl;
return 0;
}思路2:dp
设原始数列为arr[1~n] 设dp[1~n]数组,初始值为1
遍历数组2~n,若a[i−1]∗2 ≥ a[i] ,那么dp[i]=dp[i-1]+1
AC代码:
/*
* @Author: Spare Lin
* @Project: AcWing2022
* @Date: 2022/7/2 21:10
* @Description: dp做法
*/
#include <iostream>
using namespace std;
const int MAXN = 2e5 + 7;
int dp[MAXN], arr[MAXN], n, ans = 0;
int main() {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
dp[i] = 1;
}
for (int i = 2; i <= n; ++i) {
if(arr[i] <= arr[i - 1] * 2) {
dp[i] = max(dp[i], dp[i - 1] + 1);
}
}
for (int i = 1; i <= n; ++i) {
ans = max(ans, dp[i]);
}
cout << ans << endl;
return 0;
}边栏推荐
- [JDBC] API parsing
- Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
- Build your own website (23)
- 大变局!全国房价,跌破万元大关
- Rsync remote synchronization
- 数仓任务里面 跑SQL任务的时候用的数据库账号是在哪里配置的
- Why is WPA3 security of enterprise business so important?
- The most complete postman interface test tutorial in the whole network, API interface test
- [combinatorics] recursive equation (the relationship theorem between the solution of the recursive equation and the characteristic root | the linear property theorem of the solution of the recursive e
- [combinatorial mathematics] recursive equation (example of recursive equation 2 Hanoi Tower | example of recursive equation 3 insertion sequencing)
猜你喜欢

How to train mask r-cnn model with your own data
![[RT thread] NXP rt10xx device driver framework -- Audio construction and use](/img/85/32a83eaa4b7f5b30d4d7c4f4c32791.png)
[RT thread] NXP rt10xx device driver framework -- Audio construction and use

【RT-Thread】nxp rt10xx 设备驱动框架之--Audio搭建和使用

Free data | new library online | cnopendata complete data of China's insurance intermediary outlets

【Try to Hack】主动侦查隐藏技术
![[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework](/img/df/a7719bcb00ff66e21f3a391ab94573.png)
[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework

静态程序分析(一)—— 大纲思维导图与内容介绍

Depth first search of graph

Play with fancy special effects. This AE super kit is for you

New library online | cnopendata complete data of Chinese insurance institution outlets
随机推荐
How to promote cross department project collaboration | community essay solicitation
C语言按行修改文件
How SVN views modified file records
【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
Leetcode: lucky number in matrix
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
Leetcode13. Roman numeral to integer (three solutions)
Golang单元测试、Mock测试以及基准测试
跨境电商:外贸企业做海外社媒营销的优势
One brush 146 force buckle hot question-3 longest substring without repeated characters (m)
27. 输入3个整数,按从大到小的次序输出。要求用指针方法实现。
On Lagrange interpolation and its application
新库上线 | CnOpenData中国观鸟记录数据
visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
[2. Basics of Delphi grammar] 2 Object Pascal data type
Financial management (Higher Vocational College) financial management online Assignment 1 in autumn 20
How do large consumer enterprises make digital transformation?
Rsync远程同步
Analysis of variance summary
How to delete a specific line from a text file using the SED command?