poj - 3039 Margaritas on the River Walk
2012-08-29 00:48
375 查看
稍微复杂一些的0、1背包,要把背包尽可能装满,直到再也装不下任何物品,求总装法。这题是不用考虑W的,先把V升序排序,从小到大枚举,当前物品为剩下未使用的物品中体积最小的,那么比当前小的一定是要使用的。这种思路是n^3的,后面的物品会被重复计算,如果从后往前枚举就能避免这个问题,复杂度为n^2。
#include <stdio.h> #include <string.h> #include <algorithm> using namespace std; int dp[1005],sum[35],w[35]; int main() { int T,m,n,v,i,j,k,ans; scanf("%d",&T); for(m = 1; m <= T; m++) { scanf("%d%d",&n,&v); for(i = 1; i <= n; i++) scanf("%d",&w[i]); sort(w+1,w+1+n); if(w[1] > v) { printf("%d 0\n",m); continue; } ans = 0; sum[0] = 0; for(i = 1; i <= n; i++) sum[i] = sum[i-1] + w[i]; memset(dp,0,sizeof dp); dp[0] = 1; for(i = n; i >= 1; i--) { k = max(0,v-sum[i]+1); for(j = v-sum[i-1]; j >= k; j--) if(dp[j]) ans += dp[j]; for(j= v; j >= w[i]; j--) dp[j] += dp[j - w[i] ]; } printf("%d %d\n",m,ans); } return 0; }
相关文章推荐
- POJ - 3039 MARGARITAS ON THE RIVER WALK
- POJ 3093 Margaritas on the River Walk 背包DP
- POJ 3093 Margaritas on the River Walk(背包方案统计变型)
- POJ 3093 Margaritas on the River Walk 0-1背包好题
- POJ 3093 Margaritas on the River Walk
- poj 3093 Margaritas on the River Walk(01背包统计)
- POJ 3093 Margaritas on the River Walk
- POJ 3093 Margaritas on the River Walk 0-1背包好题
- POJ 3093 Margaritas on the River Walk(背包)
- poj 3093 Margaritas on the River Walk
- POJ 3093 Margaritas on the River Walk
- POJ 3093 Margaritas on the River Walk 笔记
- POJ 3093 Margaritas on the River Walk
- pku 3039 Margaritas on the River Walk 背包dp 解题报告
- POJ 3093 Margaritas(Kind of wine) on the River Walk (背包方案统计)
- POJ 3093 Margaritas on the River Walk
- pku3093 Margaritas on the River Walk
- 【POJ3093】Margaritas on the River Walk【01背包变种】
- poj3093 Margaritas on the River Walk(特殊的01背包)
- poj[3093]Margaritas On River Walk