当前位置:网站首页>牛客网:拦截导弹
牛客网:拦截导弹
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;
}边栏推荐
- Those things I didn't know until I took the postgraduate entrance examination
- Hide Chinese name
- :: ffff:192.168.31.101 what address is it?
- With 4 years of working experience, you can't tell five ways of communication between multithreads. Dare you believe it?
- Rk3566 add LED
- 嵌入式软件架构设计-消息交互
- Nantong online communication group
- Kotlin协程利用CoroutineContext实现网络请求失败后重试逻辑
- MySQL get time
- Jasypt configuration file encryption | quick start | actual combat
猜你喜欢

zabbix 监控

Wonderful express | Tencent cloud database June issue

PHP basic syntax
![Primary code audit [no dolls (modification)] assessment](/img/b8/82c32e95d1b72f75823ca91c97138e.jpg)
Primary code audit [no dolls (modification)] assessment

Redis6 transaction and locking mechanism

Usage, installation and use of TortoiseSVN

锚点导航小demo

STM32 reverse entry

Data Lake (VII): Iceberg concept and review what is a data Lake

Attack and defense world web WP
随机推荐
Source code analysis of etcd database -- peer RT of inter cluster network layer client
锚点导航小demo
leetcode 10. Regular expression matching regular expression matching (difficult)
Record in-depth learning - some bug handling
龙芯派2代烧写PMON和重装系统
Win10——轻量级小工具
这18个网站能让你的页面背景炫酷起来
Wechat app payment callback processing method PHP logging method, notes. 2020/5/26
What are the private addresses
Jetpack Compose入门到精通
PostgreSQL Usage Summary (PIT)
Primary code audit [no dolls (modification)] assessment
Embedded software architecture design - message interaction
What happened to the communication industry in the first half of this year?
Don't know these four caching modes, dare you say you understand caching?
南理工在线交流群
When there are too many input boxes such as input transmitted at one time in the form, the post data is intercepted
redis6事务和锁机制
stm32逆向入门
ELFK部署