当前位置:网站首页>Longest ascending subsequence model acwing 1010. Interceptor missile
Longest ascending subsequence model acwing 1010. Interceptor missile
2022-07-27 11:13:00 【T_ Y_ F666】
Longest ascending subsequence model AcWing 1010. Interceptor missile
Original link
AcWing 1010. Interceptor missile
Algorithm tags
DP linear DP Longest ascending subsequence
Ideas
Code
#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] Indicates the... At this time i The altitude of the last missile intercepted by the missile interception system
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 Represents the length of the existing subsequence
rep(i, 0, n){
a[i]=stoi(v[i]);
}
// With i Ending descent sequence
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] Create a new interception system
if(k==cnt){
f1[cnt++]=a[i];
}
// a[i] Be the first k The last Missile Altitude of the interceptor system
else{
f1[k]=a[i];
}
}
printf("%lld\n%lld\n", ans, cnt);
}
y Master code
#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
Read in processing
Input stream advanced syntax
string line;
getline(cin, line);
stringstream ssin(line);
while (ssin >> h[n]) n ++ ;
or
while (cin >> h[ ++ n]);
边栏推荐
猜你喜欢
随机推荐
Object array de duplication
Use of beautifulsoup
Non progressive phenomena of entropy and morphology
The open source project Taier version 1.1 was officially released, and the list of new functions is fast
最长上升子序列模型 AcWing 1016. 最大上升子序列和
Learning notes uni app
Yonbuilder enables innovation, and the "golden keyboard Award" of the fourth UFIDA developer competition is open!
BeautifulSoup的使用
Remember not to copy your group work, students. Fortunately, you only passed two questions. Don't have an accident
Miscellaneous records of Finance
Sorry, you guys have something to deal with in the bank recently, which has been delayed
8 find subsequences with a maximum length of K
洛谷P1441 砝码称重
A verification test of the relationship between iteration number and entropy
正则form表单判断
Compete for the key battle of stock users and help enterprises build a perfect labeling system - 01 live review
antd table中排序th阻止悬停变色+table悬停行变色+table表头变色
IMG SRC is empty or SRC does not exist, and the picture has a white border
Budweiser, a well-known beer, plans to launch NFT in an attempt to unveil the "long planned" uplink?
I've compromised. Since everyone wants to call me Yelin, there's nothing I can do









