当前位置:网站首页>牛客网:拦截导弹
牛客网:拦截导弹
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;
}边栏推荐
- Self built shooting range 2022
- Source code analysis of etcd database -- peer RT of inter cluster network layer client
- NFT value and white paper acquisition
- ZABBIX monitoring
- asp. Net read TXT file
- Primary code audit [no dolls (modification)] assessment
- Programmer growth Chapter 8: do a good job of testing
- 【公开课预告】:视频质量评价基础与实践
- Pancake Bulldog robot V2 (code optimized)
- 2022司钻(钻井)考试题库及模拟考试
猜你喜欢

Laravel framework operation error: no application encryption key has been specified

NFT value and white paper acquisition

Attack and defense world web WP

锚点导航小demo

龙芯派2代烧写PMON和重装系统

stm32逆向入门

Redis6 transaction and locking mechanism

Usage, installation and use of TortoiseSVN

What are the private addresses

Embedded software architecture design - message interaction
随机推荐
About the problem and solution of 403 error in wampserver
Record in-depth learning - some bug handling
Laravel generate entity
Liar report query collection network PHP source code
2022年机修钳工(高级)考试题模拟考试题库模拟考试平台操作
如何把大的‘tar‘存档文件分割成特定大小的多个文件
内网穿透工具 netapp
[server data recovery] a case of RAID5 data recovery stored in a brand of server
Kafaka log collection
Solve the problem of invalid uni app configuration page and tabbar
Network security HSRP protocol
Jasypt configuration file encryption | quick start | actual combat
Ordering system based on wechat applet
Win10——轻量级小工具
aspx 简单的用户登录
华为推送服务内容,阅读笔记
MySQL if else use case use
redis6主从复制及集群
Laravel framework operation error: no application encryption key has been specified
Embedded software architecture design - message interaction