当前位置:网站首页>牛客网:拦截导弹
牛客网:拦截导弹
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;
}
边栏推荐
- 网络安全-HSRP协议
- 基于微信小程序的订餐系统
- Laravel generate entity
- Zhubo Huangyu: it's really bad not to understand these gold frying skills
- About the problem and solution of 403 error in wampserver
- Kafaka log collection
- Idea set method annotation and class annotation
- Etcd database source code analysis -- rawnode simple package
- Integer = = the comparison will unpack automatically. This variable cannot be assigned empty
- Basic characteristics and isolation level of transactions
猜你喜欢
Kotlin协程利用CoroutineContext实现网络请求失败后重试逻辑
jasypt配置文件加密|快速入门|实战
Attack and defense world web WP
ELFK部署
Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
Jetpack Compose入门到精通
The "Baidu Cup" CTF competition was held in February 2017, Web: explosion-2
Solve the problem of invalid uni app configuration page and tabbar
嵌入式软件架构设计-消息交互
When using Tencent cloud for the first time, you can only use webshell connection instead of SSH connection.
随机推荐
Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
ELK 企业级日志分析系统
Data Lake (VII): Iceberg concept and review what is a data Lake
[cloud resources] what software is good for cloud resource security management? Why?
These 18 websites can make your page background cool
What about data leakage? " Watson k'7 moves to eliminate security threats
法国学者:最优传输理论下对抗攻击可解释性探讨
STM32 reverse entry
Parsing XML using Dom4j
华为推送服务内容,阅读笔记
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
【云资源】云资源安全管理用什么软件好?为什么?
Liar report query collection network PHP source code
Log4j utilization correlation
Internal JSON-RPC error. {"code":-32000, "message": "execution reverted"} solve the error
几款分布式数据库的对比
荐号 | 有趣的人都在看什么?
Usage, installation and use of TortoiseSVN
Introduction to Chapter 8 proof problem of njupt "Xin'an numeral base"
【MySQL 使用秘籍】一网打尽 MySQL 时间和日期类型与相关操作函数(三)