题意

n个有体积的物品,问选取一些物品,且不能再继续选有多少方法?

n<=1000

题解

以前的考试题。当时是A了,但发现是数据水,POJ上WA了。

把体积从小到大排序枚举没选的物品中体积最小的。

假设枚举到i,那么1到i-1一定都选。可选的空间为[m-sum[i-1]+1,m]

然后对于后面的数跑DP的到f[i][j]前i个数空间恰好为j时的方案;

贡献为可选空间的方案。

一个优化是倒着枚举i,这样在求f[i][j]时所花费的时间会大大减少。

 #include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int sum[],w[];
int dp[];
int n, m, ans;
int main(){
int cas, i, j, k, ca=;
scanf("%d",&cas);
while(cas--){
scanf("%d%d",&n,&m);
for(i=;i<=n;i++)
scanf("%d",&w[i]);
sort(w+,w++n);
printf("%d ",ca++);
if(w[]>m){printf("0\n");continue;}
sum[]=;
for(i=;i<=n;i++)
sum[i]=sum[i-]+w[i];
ans=;
memset(dp,,sizeof(dp));
dp[]=;
for(i=n;i>=;i--){
for(j=max(,m-sum[i-]-w[i]+);j<=m-sum[i-];j++)
ans+=dp[j];
for(j=m;j>=w[i];j--)
dp[j]+=dp[j-w[i]];
}
printf("%d\n",ans);
}
return ;
}

POJ 3093 Margaritas on the River Walk(背包)的更多相关文章

  1. POJ 3093 Margaritas(Kind of wine) on the River Walk (背包方案统计)

    题目 Description One of the more popular activities in San Antonio is to enjoy margaritas in the park ...

  2. poj[3093]Margaritas On River Walk

    Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...

  3. Margaritas on the River Walk_背包

    Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...

  4. POJ 1636 Prison rearrangement DFS+0/1背包

    题目链接: id=1636">POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS   Memor ...

  5. POJ 1337 A Lazy Worker(区间DP, 背包变形)

    Description There is a worker who may lack the motivation to perform at his peak level of efficiency ...

  6. POJ Washing Clothes 洗衣服 (01背包,微变型)

    题意:有多种颜色的衣服,由两个人合作来洗,必须洗完一种颜色才能洗下一种,求需要多少时间能洗完. 思路:将衣服按颜色分类,对每种颜色进行01背包,容量上限是该种颜色衣服全部洗完的耗时长一半,其实就是在最 ...

  7. bzoj2287【POJ Challenge】消失之物(退背包)

    2287: [POJ Challenge]消失之物 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 657  Solved: 382[Submit][S ...

  8. [Poj 1015] Jury Compromise 解题报告 (完全背包)

    题目链接:http://poj.org/problem?id=1015 题目: 题解: 我们考虑设计DP状态(因为这很显然是一个完全背包问题不是吗?) dp[j][k]表示在外层循环到i时,选了j个人 ...

  9. POJ 1384 Piggy-Bank (ZOJ 2014 Piggy-Bank) 完全背包

    POJ :http://poj.org/problem?id=1384 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode ...

随机推荐

  1. 优动漫PAINT安装教程

    优动漫PAINT是一款漫画.插画.动画绘制软件.其功能可分别满足画师对于插画.漫画和动画创作的针对性需求,是一款非常好用易上手的动漫绘图软件,本文来看使用软件第一步,如何安装优动漫PAINT. 步骤一 ...

  2. day19-2 生成器,递归函数

    目录 生成器 有关yield的理解 实现range()函数 生成器表达式 递归 思考 斐波那契额 汉诺塔 二分法 生成器 自定义的迭代器 yield关键字: 和return一样,接收值,但不终止函数 ...

  3. jmeter+ant+jenkins持续集成

    邮件.报告插件和jenkins的war包下载地址:链接:https://pan.baidu.com/s/1gZJ53x50bxVyEsQFjdCkog 密码:1jtz 1.下载ant  网盘地址:链接 ...

  4. React和Vue中,是如何监听变量变化的

    React 中事件监听 本地调试React代码的方法 先将React代码下载到本地,进入项目文件夹后yarn build 利用create-react-app创建一个自己的项目 把react源码和自己 ...

  5. React:关于虚拟DOM(Virtual DOM)

    Virtual DOM 是一个模拟 DOM 树的 JavaScript 对象. React 使用 Virtual DOM 来渲染 UI,当组件状态 state 有更改的时候,React 会自动调用组件 ...

  6. UVALIVE 4256 Salesmen

    Salesmen Time Limit: 3000ms Memory Limit: 131072KB This problem will be judged on UVALive. Original ...

  7. HDU 4328 Contest 3

    悬线法可解,稍有点烦琐. #include <iostream> #include <cstdio> #include <cstring> #include < ...

  8. Eureka Server添加用户认证

    Eureka Server添加用户认证 学习了:http://blog.csdn.net/liuchuanhong1/article/details/54729556 注意:1,需要使用 defaul ...

  9. HDOJ 1753 大明A+B

    JAVA大数.... 大明A+B Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. 使用Java语言实现,自己主动生成10个整数(1~100,求出生成数列中的最大值和最小值,不同意使用Arrays类的sort方法

    这是考察主要的java基础,没啥难点,直接上代码,近期在准备面试,所以做一些基础的面试题练练手 public class Demo1 { public static void main(String[ ...