POJ 3093 Margaritas on the River Walk 背包DP
2017-10-05 22:07
447 查看
dalao讲解
因此我们可以背包,按照枚举最小的一个不被选中的物品进行巧妙去重#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; const int MAXN = 10000 + 10; int T, f[MAXN], n, m, sum[MAXN], w[MAXN]; int main( ) { scanf( "%d", &T ); for( register int kase = 1; kase <= T; kase++ ) { memset( f, 0, sizeof(f) ); memset( sum, 0, sizeof(sum) ); f[0] = 1; scanf( "%d%d", &n, &m ); for( register int i = 1; i <= n; i++ ) scanf( "%d", &w[i] ); sort( w + 1, w + n + 1 ); if( w[1] > m ) { printf( "%d 0\n", kase ); continue; } for( register int i = 1; i <= n; i++ ) sum[i] = sum[ i - 1 ] + w[i]; int ans = 0; for( register int i = n; i >= 1; i-- ) { // 枚举最小的没有被选到的物品 if( m - sum[ i - 1 ] >= 0 ) { //如果现在这些小于的都被选了 int minn = max( m - sum[i] + 1, 0 ); // 找到还剩多少的最小值 //现在至少要把m - sum[i] + 1 的空间全部选满,因为如果不选满,则还剩 //m - ( m - sum[i] + 1 ) = sum[i] - 1;这是剩下的空间,而sum[ i - 1 ]又必须要选,因此这个物品是选不了的 for( register int j = minn; j <= m - sum[ i - 1 ]; j++ ) ans += f[j]; } for( register int j = m; j >= w[i]; j-- ) f[j] += f[ j - w[i] ]; } printf( "%d %d\n", kase, ans ); } return 0; }
相关文章推荐
- POJ 3093 Margaritas on the River Walk 0-1背包好题
- POJ 3093 Margaritas(Kind of wine) 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
- 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 on the River Walk
- poj[3093]Margaritas On River Walk
- pku3093 Margaritas on the River Walk
- POJ - 3039 MARGARITAS ON THE RIVER WALK
- poj - 3039 Margaritas on the River Walk
- POJ3093 Margaritas on the River Wal ( 背包DP)
- POJ 3093 Margaritas on the River Walk