当前位置:网站首页>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;
}边栏推荐
- Electronic technology 20th autumn "Introduction to machine manufacturing" online assignment 3 [standard answer]
- Static program analysis (I) -- Outline mind map and content introduction
- Electronic Science and technology 20th autumn "Microcomputer Principle and application" online assignment 2 [standard answer]
- Why is WPA3 security of enterprise business so important?
- Where is the monitoring page of RDS database?
- New library online | cnopendata China bird watching record data
- RedHat 6.2 configuring ZABBIX
- Vs code plug-in korofileheader
- Kotlin learning quick start (7) -- wonderful use of expansion
- [UE4] brush Arctic pack high quality Arctic terrain pack
猜你喜欢

Kubernetes resource object introduction and common commands (4)

What is your income level in the country?

聊聊接口优化的几个方法

Simple use of unity pen XR grab

人生还在迷茫?也许这些订阅号里有你需要的答案!

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

大消费企业怎样做数字化转型?

Golang单元测试、Mock测试以及基准测试

How to train mask r-cnn model with your own data

问题随记 —— 在 edge 上看视频会绿屏
随机推荐
Open vsftpd port under iptables firewall
RF analyze demo build step by step
New features of C 10
[try to hack] active detection and concealment technology
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
One brush 148 force deduction hot question-5 longest palindrome substring (m)
Apache service suspended asynchronous acceptex failed
Assignment examination questions of advanced English (III) for the course examination of Fujian Normal University in February 2022
Solution to long waiting time of SSH connection to remote host
[2. Basics of Delphi grammar] 2 Object Pascal data type
One brush 142 monotone stack next larger element II (m)
[combinatorics] recursive equation (general solution structure of recursive equation with multiple roots | linear independent solution | general solution with multiple roots | solution example of recu
How to train mask r-cnn model with your own data
静态程序分析(一)—— 大纲思维导图与内容介绍
一位普通程序员一天工作清单
Rsync远程同步
Assembly instance analysis -- screen display in real mode
鸿蒙第三次培训
[RT thread] NXP rt10xx device driver framework -- Audio construction and use
手把手带你入门 API 开发