当前位置:网站首页>AcWing 4489. Longest subsequence
AcWing 4489. Longest subsequence
2022-07-03 17:17:00 【How far is it forever】
Original link :4489. The longest subsequence - AcWing Question bank
Given a length of nn Of Strictly monotonically increasing Integer sequence of a1,a2,…,an.
Sequence aa The subsequence of can be expressed as ai1,ai2,…,aipai1,ai2,…,aip, among 1≤i1<i2<…<ip≤n1≤i1<i2<…<ip≤n.
We hope to find one aa The subsequence , Make the subsequence satisfy : about j∈[1,p−1]j∈[1,p−1],aij+1≤aij×2 Hang up .
We think , The length is 11 The subsequence of always satisfies the condition .
Please calculate and output the maximum possible length of the subsequence that meets the conditions .
Input format
The first line contains an integer n.
The second line contains n It's an integer a1,a2,…,an.
Output format
An integer , Represents the maximum possible length of the subsequence that meets the condition .
Data range
front 5 Test points meet 1≤n≤10.
All test points meet 1≤n≤2×10^5,1≤a1<a2<…<an≤10^9.
sample input 1:
10
1 2 5 6 7 10 21 23 24 49
sample output 1:
4
sample input 2:
5
2 10 50 110 250
sample output 2:
1
sample input 3:
6
4 7 12 100 150 199
sample output 3:
3Ideas 1: greedy
AC Code :
#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;
}
}
// Pay attention to the last ans by max(ans, cnt) Because it was last updated cnt Not with ans Compare
cout << max(ans, cnt) << endl;
return 0;
}Ideas 2:dp
Let the original sequence be arr[1~n] set up dp[1~n] Array , The initial value is 1
Traversal array 2~n, if a[i−1]∗2 ≥ a[i] , that dp[i]=dp[i-1]+1
AC Code :
/*
* @Author: Spare Lin
* @Project: AcWing2022
* @Date: 2022/7/2 21:10
* @Description: dp practice
*/
#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;
}边栏推荐
- [RT thread] NXP rt10xx device driver framework -- RTC construction and use
- Applet setting multi account debugging
- Javescript variable declaration -- VaR, let, const
- How do large consumer enterprises make digital transformation?
- The difference between get and post
- HP 阵列卡排障一例
- Bcvp developer community 2022 exclusive peripheral first bullet
- 数仓任务里面 跑SQL任务的时候用的数据库账号是在哪里配置的
- ANOVA example
- How to judge the region of an IP through C?
猜你喜欢

Analysis of variance summary

Simple use of unity pen XR grab

Meituan side: why does thread crash not cause JVM crash

Why is WPA3 security of enterprise business so important?

Great changes! National housing prices fell below the 10000 yuan mark

C language modifies files by line

Applet setting multi account debugging
![[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

What is your income level in the country?

【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
随机推荐
[2. Basics of Delphi grammar] 2 Object Pascal data type
VM11289 WAService. js:2 Do not have __ e handler in component:
[combinatorics] recursive equation (definition of general solution | structure theorem of general solution of recursive equation without multiple roots)
How to delete a specific line from a text file using the SED command?
[combinatorics] recursive equation (general solution structure of recursive equation with multiple roots | linear independent solution | general solution with multiple roots | solution example of recu
数仓任务里面 跑SQL任务的时候用的数据库账号是在哪里配置的
Answer to the homework assessment of advanced English reading (II) of the course examination of Fuzhou Normal University in February 2022
新库上线 | CnOpenData中国保险机构网点全集数据
Simple configuration of postfix server
[combinatorics] recursive equation (constant coefficient linear homogeneous recursive equation | constant coefficient, linear, homogeneous concept description | constant coefficient linear homogeneous
The most complete postman interface test tutorial in the whole network, API interface test
27. Input 3 integers and output them in descending order. Pointer method is required.
Cross border e-commerce: advantages of foreign trade enterprises in overseas social media marketing
Redis: operation commands for list type data
基于主机的入侵系统IDS
Great changes! National housing prices fell below the 10000 yuan mark
Pools de Threads: les composants les plus courants et les plus sujets aux erreurs du Code d'affaires
One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
SVN完全备份svnadmin hotcopy
LeetCode13.罗马数字转整数(三种解法)