当前位置:网站首页>牛客网:拦截导弹
牛客网:拦截导弹
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;
}边栏推荐
- ZABBIX monitoring
- Could not set property 'ID' of 'class xx' with value 'XX' argument type mismatch solution
- Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
- 真正的缓存之王,Google Guava 只是弟弟
- 49. 字母异位词分组:给你一个字符串数组,请你将 字母异位词 组合在一起。可以按任意顺序返回结果列表。 字母异位词 是由重新排列源单词的字母得到的一个新单词,所有源单词中的字母通常恰好只用一次。
- Mmseg - Mutli view time series data inspection and visualization
- The real king of caching, Google guava is just a brother
- leetcode 10. Regular Expression Matching 正则表达式匹配 (困难)
- Win10 - lightweight gadget
- ::ffff:192.168.31.101 是一个什么地址?
猜你喜欢

Could not set property 'ID' of 'class xx' with value 'XX' argument type mismatch solution

ELFK部署
Jetpack Compose入门到精通

运筹说 第68期|2022年最新影响因子正式发布 快看管科领域期刊的变化
![[server data recovery] a case of RAID5 data recovery stored in a brand of server](/img/04/c9bcf883d45a1de616c4e1b19885a5.png)
[server data recovery] a case of RAID5 data recovery stored in a brand of server

研究生可以不用学英语?只要考研英语或六级分数高!

Catch all asynchronous artifact completable future

How to deal with the Yellow Icon during the installation of wampserver

STM32 reverse entry

Idea set method annotation and class annotation
随机推荐
PHP character capture notes 2020-09-14
::ffff:192.168.31.101 是一个什么地址?
When using Tencent cloud for the first time, you can only use webshell connection instead of SSH connection.
搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2
asp.net 读取txt文件
How to divide a large 'tar' archive file into multiple files of a specific size
Nantong online communication group
Resttemplate details
ELFK部署
redis6数据类型及操作总结
PHP generate Poster
asp. Net read TXT file
Can graduate students not learn English? As long as the score of postgraduate entrance examination English or CET-6 is high!
Zhubo Huangyu: these spot gold investment skills are not really bad
Selenium crawls Baidu pictures
Log4j utilization correlation
aspx 简单的用户登录
js 从一个数组对象中取key 和value组成一个新的对象
Attack and defense world web WP
Record in-depth learning - some bug handling