当前位置:网站首页>Niuke: intercepting missiles
Niuke: intercepting missiles
2022-07-05 14:20:00 【lsgoose】


This problem is transformed into the problem of finding the longest increasing subsequence through a mathematical theorem .
First , The first question is obviously to find the longest non decreasing subsequence , The second question is to use a theorem , You can refer to :
Dilworth What is the theorem _litble The blog of -CSDN Blog _dilworth Theorem
Finally, after transforming the problem , The solution is very simple , mathematics yyds, The code is as follows :
#include<bits/stdc++.h>
using namespace std;
// Find the length of the longest non decreasing subsequence
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;
}
// Find the length of the longest increasing subsequence
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;
}边栏推荐
- How can non-technical departments participate in Devops?
- Which Internet companies are worth going to in Shenzhen for software testers [Special Edition for software testers]
- R语言ggplot2可视化:可视化折线图、使用theme函数中的legend.position参数自定义图例的位置
- The forked VM terminated without saying properly goodbye
- 一网打尽异步神器CompletableFuture
- 【学习笔记】阶段测试1
- 非技术部门,如何参与 DevOps?
- 魅族新任董事长沈子瑜:创始人黄章先生将作为魅族科技产品战略顾问
- mysql 自定义函数 身份证号转年龄(支持15/18位身份证)
- Tdengine biweekly selection of community issues | phase III
猜你喜欢

TDengine 社区问题双周精选 | 第三期

Loop invariant

CyCa children's physical etiquette Ningbo training results assessment came to a successful conclusion

Redis如何实现多可用区?

LeetCode_ 2 (add two numbers)

分享 12 个最常用的正则表达式,能解决你大部分问题

Thymeleaf th:with use of local variables

Thymeleaf th:classappend attribute append th:styleappend style append th:data- custom attribute

SaaS multi tenant solution for FMCG industry to build digital marketing competitiveness of the whole industry chain

World Environment Day | Chow Tai Fook serves wholeheartedly to promote carbon reduction and environmental protection
随机推荐
VC development of non MFC program memory leak tracking code
区间 - 左闭右开
最简单不用证书也可以多开功能的方式
freesurfer运行完recon-all怎么快速查看有没有报错?——核心命令tail重定向
根据CronSequenceGenerator计算cron表达式的时间
The forked VM terminated without saying properly goodbye
判断变量是否为数组
How to call the function mode of one hand and one machine
R语言使用原生包(基础导入包、graphics)中的boxplot函数可视化箱图(box plot)
Thymeleaf 使用后台自定义工具类处理文本
03_ Dataimport of Solr
Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder
Tiflash compiler oriented automatic vectorization acceleration
What category does the Internet of things application technology major belong to
ASP. Net large takeout ordering system source code (PC version + mobile version + merchant version)
04_ Use of solrj7.3 of solr7.3
【学习笔记】图的连通性与回路
TDengine 社区问题双周精选 | 第三期
为什么我认识的机械工程师都抱怨工资低?
详解Vue适时清理keepalive缓存方案