当前位置:网站首页>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 -- pin construction and use
- SSH连接远程主机等待时间过长的解决方法
- [RT thread] NXP rt10xx device driver framework -- RTC construction and use
- Why is WPA3 security of enterprise business so important?
- Simple use of unity pen XR grab
- vs code 插件 koroFileHeader
- 2021 ICPC regional competition (Shanghai) g.edge groups (tree DP)
- AcWing 3438. 数制转换
- Apache服务挂起Asynchronous AcceptEx failed.
猜你喜欢

跨境电商:外贸企业做海外社媒营销的优势

聊聊接口优化的几个方法

POM in idea XML graying solution
![29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da](/img/1c/c655c8232de1c56203873dcf171f45.png)
29: Chapter 3: develop Passport Service: 12: develop [obtain user account information, interface]; (use VO class to package the found data to meet the requirements of the interface for the returned da

Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)

【Try to Hack】主动侦查隐藏技术

One brush 149 force deduction hot question-10 regular expression matching (H)

设计电商秒杀
![[RT thread] NXP rt10xx device driver framework -- pin construction and use](/img/75/b4f034bfe49409f76e7fd92758804e.png)
[RT thread] NXP rt10xx device driver framework -- pin construction and use

【RT-Thread】nxp rt10xx 设备驱动框架之--hwtimer搭建和使用
随机推荐
PS screen printing brush 131, many illustrators have followed suit
新库上线 | CnOpenData中国保险机构网点全集数据
手把手带你入门 API 开发
Solution to long waiting time of SSH connection to remote host
设计电商秒杀
Kotlin学习快速入门(7)——扩展的妙用
Assignment examination questions of advanced English (III) for the course examination of Fujian Normal University in February 2022
New features of C 10
[combinatorics] recursive equation (characteristic equation and characteristic root | example of characteristic equation | root formula of monadic quadratic equation)
[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]
RedHat 6.2 configuring ZABBIX
Comparison of kotlin collaboration + retro build network request schemes
问题随记 —— 在 edge 上看视频会绿屏
Why is WPA3 security of enterprise business so important?
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
[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
线程池:业务代码最常用也最容易犯错的组件
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you