当前位置:网站首页>牛客网:拦截导弹
牛客网:拦截导弹
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;
}
边栏推荐
- Integer = = the comparison will unpack automatically. This variable cannot be assigned empty
- Wonderful express | Tencent cloud database June issue
- Matlab paper chart standard format output (dry goods)
- 深拷贝真难
- Wechat app payment callback processing method PHP logging method, notes. 2020/5/26
- ETCD数据库源码分析——集群间网络层客户端peerRt
- :: ffff:192.168.31.101 what address is it?
- redis6事务和锁机制
- Aikesheng sqle audit tool successfully completed the evaluation of "SQL quality management platform grading ability" of the Academy of communications and communications
- Parsing XML using Dom4j
猜你喜欢
French scholars: the explicability of counter attack under optimal transmission theory
Catch all asynchronous artifact completable future
荐号 | 有趣的人都在看什么?
记录一下在深度学习-一些bug处理
Introduction to Chapter 8 proof problem of njupt "Xin'an numeral base"
Idea设置方法注释和类注释
Internal JSON-RPC error. {"code":-32000, "message": "execution reverted"} solve the error
PHP basic syntax
几款分布式数据库的对比
Summit review | baowanda - an integrated data security protection system driven by compliance and security
随机推荐
我为什么支持 BAT 拆掉「AI 研究院」
Redis6 transaction and locking mechanism
Don't know these four caching modes, dare you say you understand caching?
MMSeg——Mutli-view时序数据检查与可视化
Usage, installation and use of TortoiseSVN
MySQL get time
Solution to the prompt of could not close zip file during phpword use
[cloud resources] what software is good for cloud resource security management? Why?
Intranet penetration tool NetApp
Network security - Novice introduction
Datapipeline was selected into the 2022 digital intelligence atlas and database development report of China Academy of communications and communications
Win10——轻量级小工具
荐号 | 有趣的人都在看什么?
redis6主从复制及集群
多人合作项目查看每个人写了多少行代码
Can graduate students not learn English? As long as the score of postgraduate entrance examination English or CET-6 is high!
基于微信小程序的订餐系统
Ordering system based on wechat applet
These 18 websites can make your page background cool
NFT value and white paper acquisition