当前位置:网站首页>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;
}
边栏推荐
- New library online | cnopendata China bird watching record data
- Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
- 【Try to Hack】主动侦查隐藏技术
- Leetcode13. Roman numeral to integer (three solutions)
- [RT thread] construction and use of --hwtimer of NXP rt10xx device driver framework
- PHP online confusion encryption tutorial sharing + basically no solution
- 建立自己的网站(23)
- Great changes! National housing prices fell below the 10000 yuan mark
- 基于主机的入侵系统IDS
- How to judge the region of an IP through C?
猜你喜欢
What is your income level in the country?
Take you to API development by hand
The way of wisdom (unity of knowledge and action)
Prepare for the golden three silver four, 100+ software test interview questions (function / interface / Automation) interview questions. win victory the moment one raises one 's standard
Unity notes unityxr simple to use
Meituan side: why does thread crash not cause JVM crash
29:第三章:开发通行证服务:12:开发【获得用户账户信息,接口】;(使用VO类包装查到的数据,以符合接口对返回数据的要求)(在多处都会用到的逻辑,在Controller中可以把其抽成一个共用方法)
kubernetes资源对象介绍及常用命令(四)
UE4 official charging resources, with a total price of several thousand
How do large consumer enterprises make digital transformation?
随机推荐
Necessary ability of data analysis
visual studio “通常每个套接字地址(协议/网络地址/端口)只允许使用一次“
Free data | new library online | cnopendata complete data of China's insurance intermediary outlets
[combinatorics] recursive equation (example of solving recursive equation without multiple roots | complete process of solving recursive equation without multiple roots)
Define a structure fraction to represent a fraction, which is used to represent fractions such as 2/3 and 5/6
Swm32 series Tutorial 4 port mapping and serial port application
Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
企业级自定义表单引擎解决方案(十一)--表单规则引擎1
大变局!全国房价,跌破万元大关
On Lagrange interpolation and its application
[combinatorics] recursive equation (definition of general solution | structure theorem of general solution of recursive equation without multiple roots)
2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
UCORE overview
One brush 142 monotone stack next larger element II (m)
Electronic Science and technology 20th autumn "Microcomputer Principle and application" online assignment 2 [standard answer]
C language string inversion
[RT thread] NXP rt10xx device driver framework -- pin construction and use
Online assignment 3 of mobile Internet technology in the 20th autumn of electronic technology [standard answer]
Hands on in-depth learning notes (XIV) 3.7 Simple implementation of softmax regression
Play with fancy special effects. This AE super kit is for you