国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【DP】學(xué)習(xí)之背包問題

這篇具有很好參考價(jià)值的文章主要介紹了【DP】學(xué)習(xí)之背包問題。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

01背包

2. 01背包問題 - AcWing題庫

記憶化搜索

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,m;
int v[N],w[N]; 
int res;
int mem[N][N];
int dfs(int x,int spv)
{
	if(mem[x][spv]) return mem[x][spv];
	if(x>n) return mem[x][spv]=0;
	if(spv>=v[x])
	{
		return mem[x][spv]=max(dfs(x+1,spv),dfs(x+1,spv-v[x])+w[x]);
	}else return mem[x][spv]=dfs(x+1,spv);
}
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>v[i]>>w[i];
	res=dfs(1,m);
	cout<<res;
	return 0;
} 

?這里補(bǔ)個(gè)圖,DFS是自頂向下推的

dp的遞推是從下往上

?倒序遞推

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,m;
int v[N],w[N]; 
int res;
int mem[N][N];
int f[N][N];
int dfs(int x,int spv)
{
	if(mem[x][spv]) return mem[x][spv];
	if(x>n) return mem[x][spv]=0;
	if(spv>=v[x])
	{
		return mem[x][spv]=max(dfs(x+1,spv),dfs(x+1,spv-v[x])+w[x]);
	}else return mem[x][spv]=dfs(x+1,spv);
}
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>v[i]>>w[i];
//	res=dfs(1,m);
//	cout<<res;
	
//	倒序遞推f[i][j]
//	從第i個(gè)物品開始,選總體積<=j的物品的總價(jià)值的最大值 
	for(int i=n;i>=1;i--)
	{
		for(int j=0;j<=m;j++)
		{
			if(j<v[i]) f[i][j]=f[i+1][j];
			else f[i][j]=max(f[i+1][j],f[i+1][j-v[i]]+w[i]);
		}
	} 
	cout<<f[1][m];
	return 0;
} 

正序遞推

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,m;
int v[N],w[N]; 
int res;
int mem[N][N];
int f[N][N];
int dfs(int x,int spv)
{
	if(mem[x][spv]) return mem[x][spv];
	if(x<1) return mem[x][spv]=0;
	if(spv>=v[x])
	{
		return mem[x][spv]=max(dfs(x-1,spv),dfs(x-1,spv-v[x])+w[x]);
	}else return mem[x][spv]=dfs(x-1,spv);
}
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>v[i]>>w[i];
//	res=dfs(n,m);
//	cout<<res;
	
//	正序遞推f[i][j]
//	從前i個(gè)物品中選,選總體積<=j的物品的總價(jià)值的最大值 
	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<=m;j++)
		{
			if(j<v[i]) f[i][j]=f[i-1][j];
			else f[i][j]=max(f[i-1][j],f[i-1][j-v[i]]+w[i]);
		}
	} 
	cout<<f[n][m];
	return 0;
} 

空間優(yōu)化遞推

用一維數(shù)組代替二維數(shù)組?

【DP】學(xué)習(xí)之背包問題

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,m;
int v[N],w[N]; 
int res;

int f[N],g[N];
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>v[i]>>w[i];

	for(int i=1;i<=n;i++)
	{
		for(int j=0;j<=m;j++)
		{
			if(j<v[i]) g[j]=f[j];
			else g[j]=max(f[j],f[j-v[i]]+w[i]);
		}
		memcpy(f,g,sizeof(f));
	} 
	cout<<f[m];
	return 0;
} 

進(jìn)一步優(yōu)化

這里m從m走到0,的原因是只讓每個(gè)物品最多拿一次。

如果正序枚舉體積,就會讓物品被拿多次,從而違反規(guī)則,

但完全背包不用考慮這個(gè)問題,因?yàn)樗旧砭湍苣枚啻蝵

所以完全背包優(yōu)化到一維可以正序枚舉。

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,m;
int v[N],w[N]; 
int res;

int f[N];
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>v[i]>>w[i];

	for(int i=1;i<=n;i++)
	{
		for(int j=m;j>=0;j--)
		{
            //if(j<v[i])  f[j]=f[j];//可以省略
			if(j>=v[i]) f[j]=max(f[j],f[j-v[i]]+w[i]);
		}
	} 
	cout<<f[m];
	return 0;
} 

二維費(fèi)用背包問題(本質(zhì)是01背包)

8. 二維費(fèi)用的背包問題 - AcWing題庫

記憶化搜索

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,cap,we;
int v[N],m[N],w[N];
int mem[N][110][110];
int dfs(int x,int spv,int spm)
{
    if(mem[x][spv][spm]) return mem[x][spv][spm];
    
    if(x>n) return 0;
    if(spv>=v[x]&&spm>=m[x]) return mem[x][spv][spm]=max(dfs(x+1,spv,spm),dfs(x+1,spv-v[x],spm-m[x])+w[x]);
    else return mem[x][spv][spm]=dfs(x+1,spv,spm);
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>cap>>we;
    for(int i=1;i<=n;i++) cin>>v[i]>>m[i]>>w[i];
    int res=dfs(1,cap,we);
    cout<<res;
    return 0;
}

倒序遞推

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,cap,we;
int v[N],m[N],w[N];
int mem[N][110][110];
int f[N][110][110];
int dfs(int x,int spv,int spm)
{
    if(mem[x][spv][spm]) return mem[x][spv][spm];
    
    if(x>n) return 0;
    if(spv>=v[x]&&spm>=m[x]) return mem[x][spv][spm]=max(dfs(x+1,spv,spm),dfs(x+1,spv-v[x],spm-m[x])+w[x]);
    else return mem[x][spv][spm]=dfs(x+1,spv,spm);
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>cap>>we;
    for(int i=1;i<=n;i++) cin>>v[i]>>m[i]>>w[i];
    // int res=dfs(1,cap,we);
    // cout<<res;
    for(int i=n;i>=1;i--)
    {
        for(int j=0;j<=cap;j++)
        {
            for(int k=0;k<=we;k++)
            {
                if(j<v[i]||k<m[i])
                {
                    f[i][j][k]=f[i+1][j][k];
                }
                else f[i][j][k]=max(f[i+1][j][k],f[i+1][j-v[i]][k-m[i]]+w[i]);
            }
        }
    }
    cout<<f[1][cap][we];
    return 0;
}

正序+二維優(yōu)化

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
int n,cap,we;
int v[N],m[N],w[N];
int mem[N][110][110];
int f[110][110];
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>cap>>we;
    for(int i=1;i<=n;i++) cin>>v[i]>>m[i]>>w[i];
    
    for(int i=1;i<=n;i++)
    {
        for(int j=cap;j>=v[i];j--)
        {
            for(int k=we;k>=m[i];k--)
            {
               f[j][k]=max(f[j][k],f[j-v[i]][k-m[i]]+w[i]);
            }
        }
    }
    cout<<f[cap][we];
    return 0;
}

完全背包

3. 完全背包問題 - AcWing題庫

記憶化搜索

完全背包? ?和? ? ? 01背包唯一不同就在于如果當(dāng)前這個(gè)物品可以裝入,在下一次選擇中x不用+1(因?yàn)槲锲肥菬o限多的,還可以再次選擇)

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;

int n,m;
int v[N],w[N];
int mem[N][N];
int dfs(int x,int spv)
{
    if(mem[x][spv]) return mem[x][spv];
    if(x>n) return 0;
    if(spv<v[x])  return mem[x][spv]=dfs(x+1,spv);
    else return mem[x][spv]=max(dfs(x+1,spv),dfs(x,spv-v[x])+w[x]);
}
int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>v[i]>>w[i];
    int res=dfs(1,m);
    cout<<res;
    return 0;
}

正序遞推?

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;

int n,m;
int v[N],w[N];
int f[N][N];

int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>v[i]>>w[i];
    
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<=m;j++)
        {
            if(j<v[i]) f[i][j]=f[i-1][j];
            else f[i][j]=max(f[i-1][j],f[i][j-v[i]]+w[i]);
        }
    }
    cout<<f[n][m];
    return 0;
}

優(yōu)化成一維數(shù)組?

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;

int n,m;
int v[N],w[N];
int f[N];

int main()
{
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>v[i]>>w[i];
    
    for(int i=1;i<=n;i++)
    {
        for(int j=v[i];j<=m;j++)
        {
            f[j]=max(f[j],f[j-v[i]]+w[i]) ;
        }
    }
    cout<<f[m];
    return 0;
}

01? ?背包優(yōu)化到一維? ? ? :逆序枚舉體積

完全背包優(yōu)化到一維? ? ? :正序枚舉體積

習(xí)題

云剪貼板 - 洛谷 | 計(jì)算機(jī)科學(xué)教育新生態(tài) (luogu.com.cn)

NASA的食物計(jì)劃(二維費(fèi)用背包問題)

P1507 NASA的食物計(jì)劃 - 洛谷 | 計(jì)算機(jī)科學(xué)教育新生態(tài) (luogu.com.cn)

記憶化搜索

#include<bits/stdc++.h>
using namespace std;
const int N=60;
int hh,tt,n;
int h[N],t[N],k[N];
int mem[55][410][510];
int dfs(int x,int sph,int spt)
{
	if(x>n) return 0;
	if(mem[x][sph][spt]) return mem[x][sph][spt];
	if(sph>=h[x]&&spt>=t[x])
	{
		return mem[x][sph][spt]=
		max(dfs(x+1,sph,spt),dfs(x+1,sph-h[x],spt-t[x])+k[x]);
	}else return mem[x][sph][spt]=dfs[x+1][sph][spt];
}
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>hh>>tt>>n;
	for(int i=1;i<=n;i++) cin>>h[i]>>t[i]>>k[i];
	int res=dfs(1,hh,tt);
	cout<<res;

	return 0;
} 

優(yōu)化后的二維數(shù)組遞推

#include<bits/stdc++.h>
using namespace std;
const int N=60;
int hh,tt,n;
int h[N],t[N],k[N];
int f[410][510];
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>hh>>tt>>n;
	for(int i=1;i<=n;i++) cin>>h[i]>>t[i]>>k[i];
	for(int i=1;i<=n;i++)
	{
		for(int j=hh;j>=h[i];j--)
		{
			for(int l=tt;l>=t[i];l--)
			{
				f[j][l]=max(f[j][l],f[j-h[i]][l-t[i]]+k[i]);
			}
		}
	}
	cout<<f[hh][tt];
	return 0;
} 

01背包求體積恰好為M的方案數(shù)

278. 數(shù)字組合 - AcWing題庫

記憶化搜索

?Time Limit Exceeded???

要優(yōu)化成遞推

#include<bits/stdc++.h>
using namespace std;
const int N=110;
int n,m;
int a[N];
int mem[N][10010];
int dfs(int x,int spm)
{
	if(spm==0) return 1;
	if(x>n) return 0;
	if(mem[x][spm]) return mem[x][spm];
	if(spm>=a[x]) return mem[x][spm]=dfs(x+1,spm-a[x])+dfs(x+1,spm);
	else return mem[x][spm]=dfs(x+1,spm);
}
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	int res=dfs(1,m);
	cout<<res;
	return 0;
} 

一維數(shù)組遞推

#include<bits/stdc++.h>
using namespace std;
const int N=110;
int n,m;
int a[N];
int f[10010];//記錄方案數(shù) 
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++) cin>>a[i];
	
    f[0]=1;
	for(int i=1;i<=n;i++)
	{
		for(int j=m;j>=a[i];j--)
		{
			f[j]=f[j-a[i]]+f[j];
		}
	}
	cout<<f[m]; 
	return 0;
} 

完全背包求體積恰好為M的方案數(shù)

OpenJudge - 6049:買書

記憶化搜索

這個(gè)就能ac了

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int n;
int mon[5]={0,10,20,50,100};
int mem[N][N];
int dfs(int x,int spn)
{
	if(spn==0) return 1;
	if(x>4) return 0;
	if(mem[x][spn]) return mem[x][spn];
	if(spn>=mon[x]) return mem[x][spn]=dfs(x+1,spn)+dfs(x,spn-mon[x]);
	else return mem[x][spn]=dfs(x+1,spn);
}
signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n;
	if(n==0)
	{
		cout<<"0";
		return 0;
	}
	int res=dfs(1,n);
	cout<<res;
	return 0;
} 

優(yōu)化一下

#include<bits/stdc++.h>
using namespace std;
const int N=1010;
int n;
int mon[5]={0,10,20,50,100};
int f[N];

signed main()
{
	ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
	cin>>n;
	if(n==0)
	{
		cout<<"0";
		return 0;
	}
	f[0]=1;
	for(int i=1;i<=4;i++)
	{
		for(int j=mon[i];j<=n;j++)
		{
			f[j]=f[j]+f[j-mon[i]];
		}
	}
	cout<<f[n];
	return 0;
} 

背包問題求具體方案

12. 背包問題求具體方案 - AcWing題庫

#include <bits/stdc++.h>
using namespace std;
const int N=1010;
int n,m;
int f[N][N];
int v[N],w[N];

int main()
{
    ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;i++) cin>>v[i]>>w[i];
    
    for(int i=n;i;i--)
    {
    	for(int j=0;j<=m;j++)
    	{
    		f[i][j]=f[i+1][j];//不選 
    		if(j>=v[i]) f[i][j]=max(f[i][j],f[i+1][j-v[i]]+w[i]);
		}
	}
    int i=1,j=m;
    while(i<=n)
    {
    	if(j>=v[i]&&f[i+1][j-v[i]]+w[i]>=f[i+1][j])
		//如果可以選并且選了之后方案數(shù)會變多 
    	{
    		cout<<i<<" ";//那就選擇 
    		j-=v[i];//體積減少 
    		i++;
		}
		else i++;
	}
    return 0; 
}

采藥

P1048 [NOIP2005 普及組] 采藥 - 洛谷 | 計(jì)算機(jī)科學(xué)教育新生態(tài) (luogu.com.cn)

記憶化搜索

#include<bits/stdc++.h>
using namespace std;
const int N=110;
int tt,m;
int t[N],v[N];
int mem[110][1010];
int dfs(int x,int spt)
{
	if(x>m) return 0;
	if(mem[x][spt]) return mem[x][spt];
	if(t[x]<=spt)
	{
		return mem[x][spt]=max(dfs(x+1,spt),dfs(x+1,spt-t[x])+v[x]);
	}else return  mem[x][spt]=dfs(x+1,spt);
}
signed main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin>>tt>>m;
	for(int i=1;i<=m;i++) cin>>t[i]>>v[i];
	
	int res=dfs(1,tt);
	cout<<res;
	
	return 0;
} 

優(yōu)化后

#include<bits/stdc++.h>
using namespace std;
const int N=110;
int tt,m;
int t[N],v[N];
int f[1010];

signed main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin>>tt>>m;
	for(int i=1;i<=m;i++) cin>>t[i]>>v[i];
	for(int i=1;i<=m;i++)
	{
		for(int j=tt;j>=t[i];j--)
		{
			f[j]=max(f[j],f[j-t[i]]+v[i]);	
		}
	}
	cout<<f[tt];
	return 0;
} 

瘋狂的采藥

P1616 瘋狂的采藥 - 洛谷 | 計(jì)算機(jī)科學(xué)教育新生態(tài) (luogu.com.cn)

沒別的,這題要注意數(shù)據(jù)范圍?

#include<bits/stdc++.h>
using namespace std;
const int N=1e4+10;
const int M=1e7+10;
#define int long long
int tt,m;
int t[N],v[N];
int f[M];
signed main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin>>tt>>m;
	for(int i=1;i<=m;i++) cin>>t[i]>>v[i];
	for(int i=1;i<=m;i++)
	{
		for(int j=t[i];j<=tt;j++)
		{
			f[j]=max(f[j-t[i]]+v[i],f[j]);
		}
	}
	cout<<f[tt];
	return 0;
} 

5倍經(jīng)驗(yàn)日

P1802 5 倍經(jīng)驗(yàn)日 - 洛谷 | 計(jì)算機(jī)科學(xué)教育新生態(tài) (luogu.com.cn)

仔細(xì)讀題?

#include<bits/stdc++.h>
using namespace std;
const int N=1e3+10;
#define int long long
int n,x;
int l[N],w[N],u[N];
int mem[N][N];
int f[N];

signed main()
{
	ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
	cin>>n>>x;
	for(int i=1;i<=n;i++) cin>>l[i]>>w[i]>>u[i];
	
	for(int i=1;i<=n;i++)
	{
		for(int j=x;j>=0;j--)
		{
			if(j>=u[i]) f[j]=max(f[j]+l[i],f[j-u[i]]+w[i]);//服藥能打過就選擇吃藥或者不吃
			else f[j]=f[j]+l[i];
		}
	}
	cout<<5*f[x];
	return 0;
} 

寵物小精靈之收服

OpenJudge - 4978:寵物小精靈之收服文章來源地址http://www.zghlxwxcb.cn/news/detail-435358.html

#include <bits/stdc++.h>
using namespace std;
int dp[1005][505];//dp[i][j][k]:
//(第一維i被優(yōu)化掉)前i個(gè)野生小精靈中最多使用j個(gè)精靈球,
//k點(diǎn)皮卡丘的體力值,能捕捉到的最大小精靈數(shù)量。 
int b[105], d[105];//b[i]:收第i小精靈需要的精靈球球數(shù) 
//d[i]:收第i小精靈皮卡丘損失的體力值 
int main()
{
    int n, m, ka, r;
    cin >> n >> m >> ka;//n個(gè)精靈球 皮卡丘m點(diǎn)體力值 野生小精靈有ka個(gè) 
    m--;//不能把m點(diǎn)體力值都用完,最多可以使用m-1點(diǎn)體力 
    for(int i = 1; i <= ka; ++i)
        cin >> b[i] >> d[i];
        
    for(int i = 1; i <= ka; ++i)
        for(int j = n; j >= b[i]; --j)
            for(int k = m; k >= d[i]; --k)
                dp[j][k] = max(dp[j][k], dp[j-b[i]][k-d[i]]+1);//記錄精靈球個(gè)數(shù) 
                
    for(int k = 0; k <= m; ++k)//用掉的體力值可以為0,從0開始遍歷 
    {
        if(dp[n][k] == dp[n][m])//d[n][m]收服的數(shù)量肯定是最多的 
        {//一旦前面有一個(gè)跟dp[n][m]收服的數(shù)量相同,就更新答案 
            r = m + 1 - k ;//原總體力值為m+1,減去用掉的體力j 
            break;
        }   
    }
    cout << dp[n][m] << ' ' << r;
    return 0; 
}

到了這里,關(guān)于【DP】學(xué)習(xí)之背包問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 動態(tài)規(guī)劃DP之背包問題3---多重背包問題

    動態(tài)規(guī)劃DP之背包問題3---多重背包問題

    目錄 DP分析: 優(yōu)化: ?二進(jìn)制優(yōu)化 例題: ? ? ? ? 01背包是每個(gè)物品只有一個(gè),完全背包問題是每個(gè)物品有無限個(gè)。 ? ? ? ? 那么多重背包問題就是 每個(gè)物品有有限個(gè) 。 有?N?種物品和一個(gè)容量是?V?的背包。 第?i?種物品最多有?si?件,每件體積是?vi,價(jià)值是?wi。 求解

    2024年03月20日
    瀏覽(43)
  • AcWing算法提高課-1.3.11二維費(fèi)用的背包問題

    AcWing算法提高課-1.3.11二維費(fèi)用的背包問題

    點(diǎn)這里 題目描述 有 N N N 件物品和一個(gè)容量是 V V V 的背包,背包能承受的最大重量是 M M M 。 每件物品只能用一次。體積是 v i v_i v i ? ,重量是 m i m_i m i ? ,價(jià)值是 w i w_i w i ? 。 求解將哪些物品裝入背包,可使物品總體積不超過背包容量,總重量不超過背包可承受的最大

    2024年02月06日
    瀏覽(24)
  • 【算法|動態(tài)規(guī)劃 | 01背包問題No.2】AcWing 423. 采藥

    【算法|動態(tài)規(guī)劃 | 01背包問題No.2】AcWing 423. 采藥

    個(gè)人主頁:兜里有顆棉花糖 歡迎 點(diǎn)贊?? 收藏? 留言? 加關(guān)注??本文由 兜里有顆棉花糖 原創(chuàng) 收錄于專欄【手撕算法系列專欄】【AcWing算法提高學(xué)習(xí)專欄】 ??本專欄旨在提高自己算法能力的同時(shí),記錄一下自己的學(xué)習(xí)過程,希望對大家有所幫助 ??希望我們一起努力、成

    2024年02月06日
    瀏覽(93)
  • C++ DP算法,動態(tài)規(guī)劃——背包問題(背包九講)

    有N件物品和一個(gè)容量為 V V V 的背包。放入第i件物品耗費(fèi)的空間是 C i C_i C i ? ,得到的價(jià)值是 W i W_i W i ? 。 求解將哪些物品裝入背包可使價(jià)值總和最大。 這是最基礎(chǔ)的背包問題,特點(diǎn)是:每種物品僅有一件,可以選擇放或不放。 用子問題定義狀態(tài):即 F [ i , v ] F[i, v] F

    2024年02月16日
    瀏覽(30)
  • 【算法宇宙——在故事中學(xué)算法】背包dp之完全背包問題

    【算法宇宙——在故事中學(xué)算法】背包dp之完全背包問題

    學(xué)習(xí)者不靈絲相傳,而自杖明月相反,子來此事卻無得失。 盡管計(jì)算機(jī)是門嚴(yán)謹(jǐn)?shù)膶W(xué)科,但正因?yàn)閲?yán)謹(jǐn),所以要有趣味才能看得下去。在筆者的前幾篇算法類文章中,都采用了以小故事作為引入的方式來介紹算法,但在回看的時(shí)候發(fā)現(xiàn)學(xué)術(shù)味還是太濃了,完全沒有想看下去的

    2023年04月20日
    瀏覽(25)
  • DP算法應(yīng)用:背包問題解析與實(shí)現(xiàn)

    DP算法應(yīng)用:背包問題解析與實(shí)現(xiàn)

    通過詳細(xì)解析背包問題的動態(tài)規(guī)劃解法,包括狀態(tài)設(shè)計(jì)、狀態(tài)轉(zhuǎn)移方程、兩種編碼方法(自頂向下和自下而上),以及滾動數(shù)組的優(yōu)化,幫助理解和實(shí)現(xiàn)動態(tài)規(guī)劃算法。

    2023年04月08日
    瀏覽(19)
  • 【第二十五課】動態(tài)規(guī)劃:完全背包問題(acwing-3 / 公式推導(dǎo) / 思路理解 / 優(yōu)化 / c++代碼)

    【第二十五課】動態(tài)規(guī)劃:完全背包問題(acwing-3 / 公式推導(dǎo) / 思路理解 / 優(yōu)化 / c++代碼)

    目錄 思路 樸素代碼 優(yōu)化 公式推導(dǎo)上? 二維代碼? 一維代碼 公式理解上 ? 在開始看完全背包問題之前,可能需要先了解01背包及其解決辦法。 指路?? 【第二十五課】動態(tài)規(guī)劃:01背包問題(acwing-2 / 思路 / 含一維數(shù)組優(yōu)化 / c++代碼) 這個(gè)問題和01背包的區(qū)別就是 每件物品可以

    2024年03月19日
    瀏覽(45)
  • 算法第十五期——動態(tài)規(guī)劃(DP)之各種背包問題

    目錄 0、背包問題分類 1、?0/1背包簡化版 【代碼】 2、0/ 1背包的方案數(shù) 【思路】

    2023年04月09日
    瀏覽(26)
  • 【算法設(shè)計(jì)與分析基礎(chǔ)(第三版)習(xí)題答案】8.2 背包問題和記憶功能

    【算法設(shè)計(jì)與分析基礎(chǔ)(第三版)習(xí)題答案】8.2 背包問題和記憶功能

    a. 對于下列背包問題的實(shí)例,應(yīng)用自底向上動態(tài)規(guī)劃算法求解。 b. a 中的實(shí)例有多少不同的最優(yōu)子集 c. 一般來說,如何從動態(tài)規(guī)劃算法所生成的表中判斷出背包問題的實(shí)例是不是具有不止一個(gè)最優(yōu)子集? 承重量 W = 6 物品 重量 價(jià)值 1 3 25 2 2 20 3 1 15 4 4 40 5 5 50 題目中要求使用

    2023年04月08日
    瀏覽(23)
  • 0-1背包問題思路分析,重點(diǎn)解釋一維dp數(shù)組的01背包問題為什么要倒序遍歷背包,以及為什么不能先遍歷背包,只能先遍歷物品

    0-1背包問題思路分析,重點(diǎn)解釋一維dp數(shù)組的01背包問題為什么要倒序遍歷背包,以及為什么不能先遍歷背包,只能先遍歷物品

    對0-1背包問題的二維dp數(shù)組以及一維dp數(shù)組的思路分析 來源:代碼隨想錄 link 本文是我對01背包問題的理解 ,在本文中具體分析dp數(shù)組的形成過程,最核心的地方就是我對每種情況下的01背包問題給出了代碼運(yùn)行結(jié)果,便于讀者理解。 重點(diǎn)解釋了為什么一維dp數(shù)組的01背包問題

    2024年02月03日
    瀏覽(43)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包