51NOD 2072 装箱问题 背包问题 01 背包 DP 动态规划
生活随笔
收集整理的這篇文章主要介紹了
51NOD 2072 装箱问题 背包问题 01 背包 DP 动态规划
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
有一個箱子容量為 V(正整數,0<=V<=20000),同時有 n 個物品(0<n<=30),每個物品有一個體積(正整數)。
現在在 n 個物品中,任取若干個裝入箱內,使得箱子的剩余空間為最小。
?收起
輸入
輸入:一個整數v,表示箱子容量 一個整數n,表示有n個物品 接下來 n 個整數,分別表示這 n 個物品的各自體積輸出
輸出:一個整數,表示箱子最小的剩余空間輸入樣例
24 6 8 3 12 7 9 7輸出樣例
0 #include<iostream> #include<queue> #include<algorithm> #include<set> #include<cmath> #include<vector> #include<map> #include<stack> #include<bitset> #include<cstdio> #include<cstring> //---------------------------------Sexy operation--------------------------//#define cini(n) scanf("%d",&n) #define cinl(n) scanf("%lld",&n) #define cinc(n) scanf("%c",&n) #define cins(s) scanf("%s",s) #define coui(n) printf("%d",n) #define couc(n) printf("%c",n) #define coul(n) printf("%lld",n) #define speed ios_base::sync_with_stdio(0) #define file freopen("input.txt","r",stdin);freopen("output.txt","w",stdout) //-------------------------------Actual option------------------------------//#define Swap(a,b) a^=b^=a^=b #define Max(a,b) a>b?a:b #define Min(a,b) a<b?a:b #define mem(n,x) memset(n,x,sizeof(n)) #define mp(a,b) make_pair(a,b) //--------------------------------constant----------------------------------//#define INF 0x3f3f3f3f #define maxn 100005 #define esp 1e-9 using namespace std; typedef long long ll; typedef pair<int,int> PII; //------------------------------Dividing Line--------------------------------// int v,n; int a[maxn]; int dp[maxn]; int main() {cini(v),cini(n);for(int i=0; i<n; i++)cini(a[i]);for(int i=0; i<n; i++){for(int j=v;j>=a[i];j--){dp[j]=max(dp[j],dp[j-a[i]]+a[i]);}}cout<<v-dp[v]<<endl; }?
總結
以上是生活随笔為你收集整理的51NOD 2072 装箱问题 背包问题 01 背包 DP 动态规划的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 51 NOD 1049 最大子段和 动
- 下一篇: 高中生分班前磕头跪谢班主任:网友看后意见