题目:2. 01背包问题
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=2e5+10;
int n,V;
int f[1010];
int main() {
scanf("%d%d",&n,&V);
int v,w;
for(int i=1;i<=n;i++){
cin>>v>>w;
for(int j=V;j>=v;j--){
f[j]=max(f[j],f[j-v]+w);
}
}
cout<<f[V];
return 0;
}
题目:3. 完全背包问题
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=2e5+10;
int n,v;
int f[1010];
int main() {
cin>>n>>v;
int x,y;
for(int i=1;i<=n;i++){
cin>>x>>y;
for(int j=x;j<=v;j++){
f[j]=max(f[j],f[j-x]+y);
}
}
cout<<f[v];
return 0;
}
题目:4. 多重背包问题 I
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=2e5+10;
int n,V;
int f[1010];
int main() {
cin>>n>>V;
int v,w,s;
for(int i=1;i<=n;i++){
cin>>v>>w>>s;
for(int j=1;j<=s;j++){
for(int k=V;k>=v;k--){
f[k]=max(f[k],f[k-v]+w);
}
}
}
cout<<f[V];
return 0;
}
题目:5. 多重背包问题 II
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=2e5+10;
int n,V;
int f[2010];
int cnt=0;
int va[12000],we[12000];
int main() {
cin>>n>>V;
int v,w,s;
for(int i=1;i<=n;i++){
scanf("%d%d%d",&v,&w,&s);
int k=1;
while(k<=s){
va[cnt]=k*v;
we[cnt++]=k*w;
s-=k;
k<<=1;
}
if(s){
va[cnt]=s*v;
we[cnt++]=s*w;
}
}
for(int i=0;i<cnt;i++){
for(int j=V;j>=va[i];j--){
f[j]=max(f[j],f[j-va[i]]+we[i]);
}
}
cout<<f[V];
return 0;
}
题目:9. 分组背包问题
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=2e5+10;
int n,V;
int va[110][110],we[110][110],s[110];
int f[110];
int main() {
cin>>n>>V;
for(int i=1;i<=n;i++){
scanf("%d",&s[i]);
for(int j=1;j<=s[i];j++){
scanf("%d%d",&va[i][j],&we[i][j]);
}
}
for(int i=1;i<=n;i++){
for( int j=V;j>=0;j--){
for(int k=1;k<=s[i];k++){
if(j>=va[i][k]){
f[j]=max(f[j],f[j-va[i][k]]+we[i][k]);
}
}
}
}
cout<<f[V];
return 0;
}
题目:898. 数字三角形
方法一:从上往下
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=2e5+10;
int n;
int f[510][510];
int main() {
cin>>n;
memset(f,-0x3f,sizeof f);
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
scanf("%d",&f[i][j]);
}
}
f[0][0]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
f[i][j]+=max(f[i-1][j-1],f[i-1][j]);
}
}
int res=-0x3f3f3f3f;
for(int i=1;i<=n;i++){
res=max(res,f[n][i]);
}
cout<<res;
return 0;
}
方法二:从下往上,更简便
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=2e5+10;
int n;
int f[510][510];
int main() {
cin>>n;
for(int i=1;i<=n;i++){
for(int j=1;j<=i;j++){
scanf("%d",&f[i][j]);
}
}
for(int i=n-1;i>=1;i--){
for(int j=1;j<=i;j++){
f[i][j]+=max(f[i+1][j],f[i+1][j+1]);
}
}
cout<<f[1][1];
return 0;
}
题目:895. 最长上升子序列
题解:时间复杂度0(n^2)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=2e5+10;
int n;
int a[1010];
int f[1010];
int main() {
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int res=0;
for(int i=1;i<=n;i++){
f[i]=1;
int j=i-1;
while(j>0){
if(a[j]<a[i]){
f[i]=max(f[i],f[j]+1);
}
j--;
}
res=max(res,f[i]);
}
cout<<res;
return 0;
}
题目:896. 最长上升子序列 II
题解:时间复杂度0(nlogn)
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=1e5+10;
int n;
int a[N];
int f[N];
int main() {
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
int cnt=0;
for(int i=1;i<=n;i++){
int l=0,r=cnt;
while(l<r){
int mid=l+r+1>>1;
if(f[mid]<a[i]) l=mid;
else r=mid-1;
}
f[l+1]=a[i];
cnt=max(cnt,l+1);
}
cout<<cnt;
return 0;
}
题目:897. 最长公共子序列
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=1e5+10;
int n,m;
char a[1010],b[1010];
int f[1010][1010];
int main() {
cin>>n>>m;
cin>>a+1>>b+1;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i]==b[j]){
f[i][j]=max(f[i][j],f[i-1][j-1]+1);
}
f[i][j]=max(f[i][j],f[i-1][j]);
f[i][j]=max(f[i][j],f[i][j-1]);
}
}
cout<<f[n][m];
return 0;
}
题目:902. 最短编辑距离
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=1e5+10;
int n,m;
char a[1010],b[1010];
int f[1010][1010];
int main() {
cin>>n;
cin>>a+1;
cin>>m;
cin>>b+1;
memset(f,0x3f,sizeof f);
for(int i=0;i<=m;i++){
f[0][i]=i;
}
for(int i=0;i<=n;i++){
f[i][0]=i;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
if(a[i]==b[j]){
f[i][j]=min(f[i][j],f[i-1][j-1]);
}else{
f[i][j]=min(f[i][j],f[i-1][j-1]+1);
}
f[i][j]=min(f[i][j],f[i-1][j]+1);
f[i][j]=min(f[i][j],f[i][j-1]+1);
}
}
cout<<f[n][m];
return 0;
}
题目:899. 编辑距离
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=1e5+10;
int n,m;
char a[1010][15];
char x[15];
int cnt;
int main() {
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>a[i]+1;
}
//for(int i=1;i<=n;i++){
// cout<<a[i]+1<<endl;
//}
for(int i=1;i<=m;i++){
cin>>x+1>>cnt;
//cout<<x+1<<endl<<cnt;
int ans=0;
for(int j=1;j<=n;j++){
int f[15][15];
memset(f,0x3f,sizeof f);
int len1=strlen(a[j]+1);
int len2=strlen(x+1);
for(int k=0;k<=len1;k++)
f[k][0]=k;
for(int k=0;k<=len2;k++)
f[0][k]=k;
for(int k=1;k<=len1;k++){
for(int l=1;l<=len2;l++){
if(a[j][k]==x[l]){
f[k][l]=min(f[k][l],f[k-1][l-1]);
}else{
f[k][l]=min(f[k][l],f[k-1][l-1]+1);
}
f[k][l]=min(f[k][l],f[k][l-1]+1);
f[k][l]=min(f[k][l],f[k-1][l]+1);
}
}
if(cnt>=f[len1][len2]) ans++;
}
cout<<ans<<endl;
}
return 0;
}