版本比较
wangxiaohe2024 模板记录
正文
增加 42 行删除 25 行
283 行未改动全部展开284285286287288288289290291103 行未改动全部展开395396397398400401402399400401402403404405406407408409410411412413414415408409410411412417418419420421413414415423424416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445426427428429430446447448432449450451452453844 行未改动全部展开283 行未改动全部展开 return 0;}```
# DP## 背包### 01背包(采药)```cpp103 行未改动全部展开 cout<<dp[0][m]; return 0;}```## 最长公共子序列(没做)
## 最长上升子序列(导弹拦截)## 最长公共子序列```cpp#include<bits/stdc++.h>using namespace std;#define ll long longmap<int,int>mp;int a[100001],b[100001],f[100001];signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); int a[100001],n=0,x; vector<int>h; while(cin>>x){ a[++n]=x; if(cin.get()=='\n') break; } cin.tie(nullptr); cout.tie(nullptr); int n; cin>>n; for(int i=1;i<=n;i++){ if(h.empty()||a[i]<=h.back()){ h.push_back(a[i]); continue; } *upper_bound(h.begin(),h.end(),a[i],greater<int>())=a[i]; cin>>a[i]; mp[a[i]]=i; } cout<<h.size()<<endl; h.clear(); for(int i=1;i<=n;i++) cin>>b[i]; memset(f,0x3f,sizeof(f)); f[0]=0; int ans=0; for(int i=1,x,pos;i<=n;i++){ auto now=mp.find(b[i]); if(now==mp.end()) continue; x=mp[b[i]],pos=lower_bound(f+1,f+n+1,x)-f; ans=max(ans,pos),f[pos]=min(x,f[pos]); } cout<<ans; return 0;}```## 最长上升子序列```cpp#include<bits/stdc++.h>using namespace std;#define ll long longmap<int,int>mp;int a[100001],b[100001];vector<int>h;signed main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); int n; cin>>n; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=n;i++){ if(h.empty()||a[i]>h.back()){ h.push_back(a[i]); continue; } *lower_bound(h.begin(),h.end(),a[i])=a[i]; if(h.empty()||a[i]>h.back()) h.push_back(a[i]); else *lower_bound(h.begin(),h.end(),a[i])=a[i]; } cout<<h.size()<<endl; cout<<h.size(); return 0;}```## 区间DP(石子合并)844 行未改动全部展开