Acwing算法基础课 第五讲 动态规划

题目: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;
}

题目:282. 石子合并

在这里插入图片描述
题解:区间连续问题,大问题的最优解由子问题的最优解去进行优化,且区间还需遍历一遍,进行最优解优化

#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[310];
int f[310][310]; 
int main() {
	cin>>n;
	for(int i=1;i<=n;i++){
		scanf("%d",&a[i]);
		a[i]+=a[i-1];
	}
	memset(f,0x3f,sizeof f);
	for(int len=1;len<=n;len++){
		for(int i=1;i+len-1<=n;i++){
			int j=i+len-1;
			if(len==1) f[i][j]=0;
			for(int k=i;k<j;k++){
				f[i][j]=min(f[i][j],f[i][k]+f[k+1][j]+a[j]-a[i-1]);
			}
		}
	}
	cout<<f[1][n];
	return 0;
}

题目:900. 整数划分

在这里插入图片描述
方法一:等价于完全背包,将1~n的数无限次的放进容量为n的背包中,看有多少种方法

#include <bits/stdc++.h> 
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=1e5+10; 
const int mod=1e9+7; 
int n;
LL f1[1010][1010];
LL f[1010];
int main() {
	cin>>n;
	f[0]=1;
	for(int i=1;i<=n;i++){
		for(int j=i;j<=n;j++){
			f[j]=(f[j]+f[j-i])%mod;
		}
	}
	printf("%lld",(f[n]%mod+mod)%mod);
	return 0;
}

方法二:等价于将n个苹果放在1~n个盘子里的方法总数。f[i][j]:i表示苹果的数量,j表示盘子的数量。

#include <bits/stdc++.h> 
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=1e5+10; 
const int mod=1e9+7; 
int n;
LL f[1010][1010];

int main() {
	cin>>n;
	f[0][0]=1;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=i;j++){
			f[i][j]=(f[i][j]+f[i-1][j-1])%mod;//增加一个只有1个苹果的盘子
			f[i][j]=(f[i][j]+f[i-j][j])%mod;//让每个盘子里苹果的数量都加1
		}
	}
	LL res=0;
	for(int i=1;i<=n;i++){
		res=(res+f[n][i])%mod;
	}
	printf("%lld",(res%mod+mod)%mod);
	return 0;
}

题目:901. 滑雪

在这里插入图片描述
在这里插入图片描述
题解:dfs+剪枝。记忆化搜索

#include <bits/stdc++.h> 
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=1e5+10; 
const int mod=1e9+7; 
int r,c;
int a[310][310];
int f[310][310];
int fx[4]={0,0,1,-1};
int fy[4]={1,-1,0,0};
int dfs(int x,int y){
	if(f[x][y]) return f[x][y];
	int res=1;
	for(int i=0;i<4;i++){
		int xx=x+fx[i];
		int yy=y+fy[i];
		if(xx<1||xx>r||yy<1||yy>c)
			continue;
		if(a[xx][yy]<a[x][y]){
			res=max(res,dfs(xx,yy)+1);
		}
	}
	return f[x][y]=res;
}
int main() {
	cin>>r>>c;
	for(int i=1;i<=r;i++){
		for(int j=1;j<=c;j++){
			cin>>a[i][j];
		}
	}
	int ans=0;
	for(int i=1;i<=r;i++){
		for(int j=1;j<=c;j++){
			if(!f[i][j]){
				ans=max(ans,dfs(i,j));
			}
		}
	}
	cout<<ans;
	return 0;
}

题目:285. 没有上司的舞会

在这里插入图片描述
在这里插入图片描述
题解:树形dp,对于每一个职员都有两种可能,参加或者不参加。最后我们在BOSS的参加和不参加里面找到最大的。

#include <bits/stdc++.h> 
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=1e5+10; 
const int mod=1e9+7; 
int n;
int a[6010];
vector<int > g[6010];
bool sta[6010];
LL f[6010][2];
void dfs(int u){
	f[u][1]=a[u];
	for(int i=0;i<g[u].size();i++){
		int t=g[u][i];
		dfs(t);
		f[u][0]+=max(f[t][0],f[t][1]);
		f[u][1]+=f[t][0];
	}
}
int main() {
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
	}
	int x,y;
	for(int i=1;i<n;i++){
		cin>>x>>y;
		g[y].push_back(x);
		sta[x]=1;
	}
	int root=1;
	while(sta[root]) root++;
	dfs(root);
	printf("%lld",max(f[root][1],f[root][0]));
	return 0;
}

题目:91. 最短Hamilton路径

在这里插入图片描述
题解:二进制+状态压缩dp

#include <bits/stdc++.h> 
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=(1<<20)+10; 
const int mod=1e9+7; 
int n;
int a[25][25];
int f[N][25];
int main() {
	cin>>n;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			cin>>a[i][j];
		}
	}
	
	memset(f,0x3f,sizeof f);
	f[1][0]=0;
	for(int i=1;i<(1<<n);i++){
		for(int j=0;j<n;j++){
			if(i>>j&1){
				for(int k=0;k<n;k++){
					if(k!=j&&(i>>k&1)){
						f[i][j]=min(f[i][j],f[i-(1<<j)][k]+a[k][j]);
//像这种1<<j的操作,最好要用括号括起来
					}
				}
			}
		}
	}
	cout<<f[(1<<n)-1][n-1];
	return 0;
}

题目:291. 蒙德里安的梦想

在这里插入图片描述
题解:状态压缩dp

#include <bits/stdc++.h> 
using namespace std;
typedef long long LL;
typedef pair<int ,int >PII;
const int N=(1<<11)+10; 
const int mod=1e9+7; 
int n,m;
vector<int > g[N];
LL f[N][15];
int main() {
	while(cin>>n>>m){
		if(n==0&&m==0) break;
		bool sta[N];
		memset(sta,0,sizeof sta);
		for(int i=0;i<(1<<n);i++){
			int cnt=0;
			bool flag=1;
			for(int j=0;j<n;j++){
				if(i>>j&1){
					if(cnt%2){
						flag=0;
						break;
					}
					cnt=0;
				}else
					cnt++;
			}
			if(cnt%2){
				flag=0;
			}
			if(flag) sta[i]=1;
		}
		
		for(int i=0;i<(1<<n);i++){
			g[i].clear();
			for(int j=0;j<(1<<n);j++){
				if((i&j)==0&&sta[i|j]){
					g[i].push_back(j);
				}
			}
		}
		memset(f,0,sizeof f);
		f[0][0]=1;
		for(int i=1;i<=m;i++){
			for(int j=0;j<(1<<n);j++){
				for(int k=0;k<g[j].size();k++){
					f[j][i]+=f[g[j][k]][i-1];
					//建议这种看有多少种方法数的都用LL
				}
			}
		}
		cout<<f[0][m]<<endl;
	} 
	return 0;
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值