当前位置:网站首页>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;
}边栏推荐
- Deep understanding of grouping sets statements in SQL
- Hands on in-depth learning notes (XIV) 3.7 Simple implementation of softmax regression
- [combinatorics] recursive equation (the problem of solving recursive equation with multiple roots | the problem is raised)
- 匯編實例解析--實模式下屏幕顯示
- Kubernetes resource object introduction and common commands (III)
- Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
- Résolution de l'instance d'assemblage - - affichage à l'écran en mode réel
- How to delete a specific line from a text file using the SED command?
- 设计电商秒杀
- Luogu: p2685 [tjoi2012] Bridge
猜你喜欢
![Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)](/img/be/4ef38f711e7319a2cc83db2bee3a07.jpg)
Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)

建立自己的网站(23)

Meituan side: why does thread crash not cause JVM crash

【JokerのZYNQ7020】DDS_ Compiler。

Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos

UE4 official charging resources, with a total price of several thousand

C语言按行修改文件

Unity notes unityxr simple to use

線程池:業務代碼最常用也最容易犯錯的組件

ucore概述
随机推荐
Define a structure fraction to represent a fraction, which is used to represent fractions such as 2/3 and 5/6
Kotlin learning quick start (7) -- wonderful use of expansion
[2. Basics of Delphi grammar] 1 Identifiers and reserved words
RDS数据库的监测页面在哪看?
C language string inversion
Static program analysis (I) -- Outline mind map and content introduction
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
[combinatorics] recursive equation (solution of linear non-homogeneous recursive equation with constant coefficients | standard form and general solution of recursive equation | proof of general solut
新库上线 | CnOpenData中国保险机构网点全集数据
University of Electronic Science and technology, accounting computerization, spring 20 final exam [standard answer]
C语言字符串反转
Javescript variable declaration -- VaR, let, const
Answer to the homework assessment of advanced English reading (II) of the course examination of Fuzhou Normal University in February 2022
[RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework
Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
PHP online confusion encryption tutorial sharing + basically no solution
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
Squid service startup script
建立自己的网站(23)
Simple use of unity pen XR grab