当前位置:网站首页>牛客网:拦截导弹
牛客网:拦截导弹
2022-07-05 13:49:00 【lsgoose】


这题通过一个数学定理将问题转化成了求最长递增子序列的问题。
首先,第一问很明显就是求最长非递减子序列,第二问要用一个定理,可以参考:
Dilworth定理是个啥东东_litble的博客-CSDN博客_dilworth定理
最后转化了问题后,求解就显得十分的简单,数学yyds,代码如下所示:
#include<bits/stdc++.h>
using namespace std;
// 求最长非递减子序列长度
int solve1(vector<int> &height){
int len=height.size();
int res=INT_MIN;
vector<int> dp(len, 1);
for(int i=0;i<len;++i){
for(int j=0;j<i;++j){
if(height[i] <= height[j]){
dp[i]=max(dp[i], dp[j]+1);
}
}
res=max(res, dp[i]);
}
return res;
}
// 求最长递增子序列长度
int solve2(vector<int> &height){
int len=height.size();
int res=INT_MIN;
vector<int> dp(len, 1);
for(int i=0;i<len;++i){
for(int j=0;j<i;++j){
if(height[i] > height[j]){
dp[i]=max(dp[i], dp[j]+1);
}
}
res=max(res, dp[i]);
}
return res;
}
int main(){
int n;
cin>>n;
vector<int> height(n);
for(int i=0;i<n;++i){
cin>>height[i];
}
int res1=solve1(height);
int res2=solve2(height);
cout<<res1<<endl<<res2<<endl;
return 0;
}边栏推荐
- 什么叫做信息安全?包含哪些内容?与网络安全有什么区别?
- redis6数据类型及操作总结
- Solve the problem of "unable to open source file" xx.h "in the custom header file on vs from the source
- Ordering system based on wechat applet
- 【公开课预告】:视频质量评价基础与实践
- 搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
- Etcd database source code analysis -- rawnode simple package
- js 从一个数组对象中取key 和value组成一个新的对象
- Win10 - lightweight gadget
- Liar report query collection network PHP source code
猜你喜欢

Set up a website with a sense of ceremony, and post it to the public 2/2 through the intranet

The real king of caching, Google guava is just a brother

荐号 | 有趣的人都在看什么?

Usage, installation and use of TortoiseSVN

Idea set method annotation and class annotation
![Primary code audit [no dolls (modification)] assessment](/img/b8/82c32e95d1b72f75823ca91c97138e.jpg)
Primary code audit [no dolls (modification)] assessment

我为什么支持 BAT 拆掉「AI 研究院」

法国学者:最优传输理论下对抗攻击可解释性探讨

Record in-depth learning - some bug handling
Jetpack compose introduction to mastery
随机推荐
uplad_ Labs first three levels
【云资源】云资源安全管理用什么软件好?为什么?
Huawei push service content, read notes
Primary code audit [no dolls (modification)] assessment
When using Tencent cloud for the first time, you can only use webshell connection instead of SSH connection.
Embedded software architecture design - message interaction
Could not set property 'ID' of 'class xx' with value 'XX' argument type mismatch solution
ELK 企业级日志分析系统
Interviewer soul torture: why does the code specification require SQL statements not to have too many joins?
The real king of caching, Google guava is just a brother
PostgreSQL Usage Summary (PIT)
How to apply the updated fluent 3.0 to applet development
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
stm32逆向入门
PHP character capture notes 2020-09-14
真正的缓存之王,Google Guava 只是弟弟
Godson 2nd generation burn PMON and reload system
蓝桥杯学习2022.7.5(上午)
Zibll theme external chain redirection go page beautification tutorial
通讯录(链表实现)