当前位置:网站首页>最长上升子序列模型 AcWing 1010. 拦截导弹
最长上升子序列模型 AcWing 1010. 拦截导弹
2022-07-27 10:35:00 【T_Y_F666】
最长上升子序列模型 AcWing 1010. 拦截导弹
原题链接
算法标签
DP 线性DP 最长上升子序列
思路
代码
#include<bits/stdc++.h>
#define int long long
#define rep(i, a, b) for(int i=a;i<b;++i)
#define Rep(i, a, b) for(int i=a;i>=b;--i)
using namespace std;
const int N = 1005, INF = 0x3f3f3f3f;
// f1[i]表示此时第i套导弹拦截系统所拦截的最后一个导弹的高度
int f[N], f1[N], a[N];
inline int read(){
int s=0,w=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
while(ch>='0'&&ch<='9') s=s*10+ch-'0',ch=getchar();
return s*w;
}
void put(int x) {
if(x<0) putchar('-'),x=-x;
if(x>=10) put(x/10);
putchar(x%10^48);
}
vector<string> get(string str){
vector<string> res;
string word;
for (auto c: str){
if (c == ' '){
res.push_back(word);
word = "";
}
else word += c;
}
res.push_back(word);
return res;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
string s;
getline(cin, s);
vector<string> v=get(s);
int ans=0, cnt=0, n=v.size();// cnt代表现有子序列长度
rep(i, 0, n){
a[i]=stoi(v[i]);
}
// 以i结尾下降序列
rep(i, 0, n){
f[i]=1;
rep(j, 0, i){
if(a[j]>=a[i]){
f[i]=max(f[i], f[j]+1);
}
}
ans=max(ans, f[i]);
int k=0;
while(k<cnt&&f1[k]<a[i]){
k++;
}
// a[i]开创一套新拦截系统
if(k==cnt){
f1[cnt++]=a[i];
}
// a[i]成为第k套拦截系统最后一个导弹高度
else{
f1[k]=a[i];
}
}
printf("%lld\n%lld\n", ans, cnt);
}
y总代码
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 1010;
int n;
int h[N], f[N], q[N];
int main()
{
string line;
getline(cin, line);
stringstream ssin(line);
while (ssin >> h[n]) n ++ ;
int res = 0, cnt = 0;
for (int i = 0; i < n; i ++ )
{
f[i] = 1;
for (int j = 0; j < i; j ++ )
if (h[i] <= h[j])
f[i] = max(f[i], f[j] + 1);
res = max(res, f[i]);
int k = 0;
while (k < cnt && q[k] < h[i]) k ++ ;
if (k == cnt) q[cnt ++ ] = h[i];
else q[k] = h[i];
}
printf("%d\n", res);
printf("%d\n", cnt);
return 0;
}
tips
读入处理
输入流高级语法
string line;
getline(cin, line);
stringstream ssin(line);
while (ssin >> h[n]) n ++ ;
或
while (cin >> h[ ++ n]);
边栏推荐
- C语言 2:求三数字最大值,求三数字中间值,编写程序步骤
- Analysis of C language pointer function and function pointer
- 基于FPGA的ECG信号采集,存储以及传输系统verilog实现
- The article will not keep VIP charges all the time. It will be open for a period of time
- Data assets are king. How to analyze the relationship between enterprise digital transformation and data asset management?
- 推导STO双中心动能积分的详细展开式
- NFT leaderboard -nft real offer latest address: NFT leaderboard.com
- 349两个数组的交集和01两数之和
- 7 row K with the weakest combat effectiveness in the matrix
- Error: image clipToBoundsAndScale, argument 'input'
猜你喜欢

Chengying, kangaroo cloud one-stop fully automated operation and maintenance steward, is officially open source

parsel的使用

Redis+caffeine two-level cache enables smooth access speed

antd table中排序th阻止悬停变色+table悬停行变色+table表头变色

Wenzhou University X kangaroo cloud: how to "know well" in the construction of higher talent education

A verification test of the relationship between iteration number and entropy

The open source project Taier version 1.1 was officially released, and the list of new functions is fast

SQL Server2000数据库错误

Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?

ASP. Net core dependency injection journey: 1. Theoretical concepts
随机推荐
Budweiser, a well-known beer, plans to launch NFT in an attempt to unveil the "long planned" uplink?
基于FPGA的ECG信号采集,存储以及传输系统verilog实现
洛谷P1896 互不侵犯
SQL Server2000 database error
How to modify the strict mode under MySQL so that adding new users by inserting user table is successful
IO流_字符流、IO流小结、IO流案例总结
学习笔记-微信支付
The article will not keep VIP charges all the time. It will be open for a period of time
Based on the open source stream batch integrated data synchronization engine Chunjun data restore DDL parsing module actual combat sharing
泰山OFFICE技术讲座:缩放比例与打开文件
49字母异位分组和242有效的字母异位词
349两个数组的交集和01两数之和
洛谷P1441 砝码称重
Time and power allocation method to ensure fairness in sensor fusion system
9 UAV array
推导STO双中心动能积分的详细展开式
Derivation of the detailed expansion sto overlap integrals
Detailed explanation of status code meaning
Overview of data security in fog computing
How to build a real-time development platform to deeply release the value of enterprise real-time data?
