当前位置:网站首页>最长上升子序列模型 AcWing 1014. 登山
最长上升子序列模型 AcWing 1014. 登山
2022-07-07 12:08:00 【T_Y_F666】
最长上升子序列模型 AcWing 1014. 登山
原题链接
算法标签
DP 线性DP 最长上升子序列
思路
分别求以i结尾上升序列,以i结尾反向上升即下降序列,以i为分割点先上升后下降序列,求最大值即可。
代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
const int N = 1005, INF = 0x3f3f3f3f;
int f[N], f1[N], a[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n=read();
rep(i, 1, n+1){
a[i]=read();
}
int ans=0, ans1=0, ans2=0;
// 以i结尾上升序列
rep(i, 1, n+1){
f[i]=1;
rep(j, 1, i){
if(a[j]<a[i]){
f[i]=max(f[i], f[j]+1);
}
}
ans1=max(ans1, f[i]);
}
// 以i结尾反向上升即下降序列
Rep(i, n, 0){
f1[i]=1;
Rep(j, n, i+1){
if(a[j]<a[i]){
f1[i]=max(f1[i], f1[j]+1);
}
}
ans2=max(ans2, f1[i]);
}
// 先上升 后下降序列
rep(i, 1, n+1){
ans=max(ans, f[i]+f1[i]-1);
}
printf("%lld\n", max({ans, ans1, ans2}));
}
与y总代码思路一致
y总代码
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n;
int h[N];
int f[N], g[N];
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i ++ ) scanf("%d", &h[i]);
for (int i = 0; i < n; i ++ )
{
f[i] = 1;
for (int j = 0; j < i; j ++ )
if (h[i] > h[j])
f[i] = max(f[i], f[j] + 1);
}
for (int i = n - 1; i >= 0; i -- )
{
g[i] = 1;
for (int j = n - 1; j > i; j -- )
if (h[i] > h[j])
g[i] = max(g[i], g[j] + 1);
}
int res = 0;
for (int i = 0; i < n; i ++ ) res = max(res, f[i] + g[i] - 1);
printf("%d\n", res);
return 0;
}
原创不易
转载请标明出处
如果对你有所帮助 别忘啦点赞支持哈
边栏推荐
- Build a secure and trusted computing platform based on Kunpeng's native security
- 566. Reshaping the matrix
- Lavarel之环境配置 .env
- AI talent cultivation new ideas, this live broadcast has what you care about
- Realization of search box effect [daily question]
- 实现IP地址归属地显示功能、号码归属地查询
- Sliding rail stepping motor commissioning (national ocean vehicle competition) (STM32 master control)
- mysql ”Invalid use of null value“ 解决方法
- 属性关键字Aliases,Calculated,Cardinality,ClientName
- 10 pictures open the door of CPU cache consistency
猜你喜欢
Custom thread pool rejection policy
Redis can only cache? Too out!
Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
室内ROS机器人导航调试记录(膨胀半径的选取经验)
2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
Detr introduction
118. Yanghui triangle
MySQL error 28 and solution
Thread pool reject policy best practices
Social responsibility · value co creation, Zhongguancun network security and Information Industry Alliance dialogue, wechat entrepreneur Haitai Fangyuan, chairman Mr. Jiang Haizhou
随机推荐
Redis can only cache? Too out!
为租客提供帮助
请问指南针股票软件可靠吗?交易股票安全吗?
Shell batch file name (excluding extension) lowercase to uppercase
The delivery efficiency is increased by 52 times, and the operation efficiency is increased by 10 times. See the compilation of practical cases of financial cloud native technology (with download)
[daily training -- Tencent select 50] 231 Power of 2
postgresql array类型,每一项拼接
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
【网络安全】sql注入语法汇总
10 pictures open the door of CPU cache consistency
Help tenants
648. 单词替换 : 字典树的经典运用
The reason why data truncated for column 'xxx' at row 1 appears in the MySQL import file
Realization of search box effect [daily question]
Dry goods | summarize the linkage use of those vulnerability tools
Is the compass stock software reliable? Is it safe to trade stocks?
属性关键字Aliases,Calculated,Cardinality,ClientName
move base参数解析及经验总结
What are the principles for distinguishing the security objectives and implementation methods that cloud computing security expansion requires to focus on?
Vmware 与主机之间传输文件