POJ 3093 Margaritas on the River Walk(背包)
题意
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(背包)的更多相关文章
- 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 ...
- poj[3093]Margaritas On River Walk
Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...
- Margaritas on the River Walk_背包
Description One of the more popular activities in San Antonio is to enjoy margaritas in the park alo ...
- POJ 1636 Prison rearrangement DFS+0/1背包
题目链接: id=1636">POJ 1636 Prison rearrangement Prison rearrangement Time Limit: 3000MS Memor ...
- POJ 1337 A Lazy Worker(区间DP, 背包变形)
Description There is a worker who may lack the motivation to perform at his peak level of efficiency ...
- POJ Washing Clothes 洗衣服 (01背包,微变型)
题意:有多种颜色的衣服,由两个人合作来洗,必须洗完一种颜色才能洗下一种,求需要多少时间能洗完. 思路:将衣服按颜色分类,对每种颜色进行01背包,容量上限是该种颜色衣服全部洗完的耗时长一半,其实就是在最 ...
- bzoj2287【POJ Challenge】消失之物(退背包)
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 657 Solved: 382[Submit][S ...
- [Poj 1015] Jury Compromise 解题报告 (完全背包)
题目链接:http://poj.org/problem?id=1015 题目: 题解: 我们考虑设计DP状态(因为这很显然是一个完全背包问题不是吗?) dp[j][k]表示在外层循环到i时,选了j个人 ...
- 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 ...
随机推荐
- CF1041E Tree Reconstruction_构造_思维题
不难发现,每次询问结果一定是 (i,n)(i,n)(i,n), 而 iii 出现的次数恰好是 iii 到 i′i'i′ 的距离(i′i'i′ 是第一个不与 iii 相等的数).我们可以将这颗树构造成一 ...
- 四、分布式 Git(未完待续)
一.分布式工作流程 在 Git 中,每个开发者同时扮演着节点和集线器的角色——也就是说,每个开发者既可以将自己的代码贡献到其他的仓库中,同时也能维护自己的公开仓库,让其他人可以在其基础上工作并贡献代码 ...
- java.lang.NoClassDefFoundError: org/apache/commons/collections4/ListValuedMap
最近在使用java PiO导入导出Excle在windos本机上运行没有问题: 但是!!问题来了!放到Linux服务器上部署后出现异常 java.lang.NoClassDefFoundError: ...
- MyEclipse背景与字体大小和xml文件中字体大小调整
1.打开window / Preference,弹出Preference面板 2.展开General标签,选中Editors选项,展开. 3.选中 Text Editors,右边出现TestEdito ...
- pytorch 3 activation 激活函数
2.3 Activation Function import torch import torch.nn.functional as F from torch.autograd import Vari ...
- RabbitMQ学习总结(2)——安装、配置与监控
一.安装 1.安装Erlang 1)系统编译环境(这里采用linux/unix 环境) ① 安装环境 虚拟机:VMware® Workstation 10.0.1 build Linux系统:Cent ...
- Ubuntu 15.10 安装推荐链接
整理一些Ubuntu 15.10系统安装使用的链接. Ubuntu官网下载:http://www.ubuntu.com/download/desktop,目前最新的版本是Ubuntu 15.10. 网 ...
- Raspberry PI 系列 —— 裸机点亮LED灯
Raspberry PI 系列 -- 裸机点亮LED灯 背景 近期刚买了Raspberry PI B+,配置执行了官方提供的Raspbian系统,折腾了一周Linux系统,感觉没啥意思,于是就试着想了 ...
- 【Oracle学习笔记】
内容主要包括: (1)三种循环及其简化 (2)游标的使用 (3)异常处理 (4)存储过程 (5)存储函数 (6)触发器 (7)其它pl/sql操作 ---------------loop循环定义变量- ...
- poj3071之概率DP
Football Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2667 Accepted: 1361 Descript ...