洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述
A little known fact about Bessie and friends is that they love stair climbing races. A better known fact is that cows really don't like going down stairs. So after the cows finish racing to the top of their favorite skyscraper, they had a problem. Refusing to climb back down using the stairs, the cows are forced to use the elevator in order to get back to the ground floor.
The elevator has a maximum weight capacity of W (1 <= W <= 100,000,000) pounds and cow i weighs C_i (1 <= C_i <= W) pounds. Please help Bessie figure out how to get all the N (1 <= N <= 18) of the cows to the ground floor using the least number of elevator rides. The sum of the weights of the cows on each elevator ride must be no larger than W.
给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组。(n<=18)
输入输出格式
输入格式:
Line 1: N and W separated by a space.
- Lines 2..1+N: Line i+1 contains the integer C_i, giving the weight of one of the cows.
输出格式:
Line 1: A single integer, R, indicating the minimum number of elevator rides needed.
- Lines 2..1+R: Each line describes the set of cows taking
one of the R trips down the elevator. Each line starts with an integer giving the number of cows in the set, followed by the indices of the individual cows in the set.
输入输出样例
4 10
5
6
3
7
3
2 1 3
1 2
1 4
说明
There are four cows weighing 5, 6, 3, and 7 pounds. The elevator has a maximum weight capacity of 10 pounds.
We can put the cow weighing 3 on the same elevator as any other cow but the other three cows are too heavy to be combined. For the solution above, elevator ride 1 involves cow #1 and #3, elevator ride 2 involves cow #2, and elevator ride 3 involves cow #4. Several other solutions are possible for this input.
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define maxn 20
int n,m,a[maxn],b[maxn],mid;
bool flag=;
void dfs(int pos,int num){
if(pos==n+){
flag=;
return;
}
if(flag)return;
for(int i=;i<=num;i++){
if(m-b[i]>=a[pos]){
b[i]+=a[pos];
dfs(pos+,num);
b[i]-=a[pos];
}
}
if(num==mid)return;
b[num+]=a[pos];
dfs(pos+,num+);
b[num+]=;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)scanf("%d",&a[i]);
int ans=n,l=,r=n;
while(l<=r){
mid=(l+r)>>;
memset(b,,sizeof(b));
flag=;
dfs(,);
if(flag)ans=mid,r=mid-;
else l=mid+;
}
printf("%d",ans);
}
100分 迭代加深搜索(二分深度)
/*
f 数组为结构体
f[S].cnt 表示集合 S 最少的分组数
f[S].v 表示集合 S 最少分组数下当前组所用的最少容量
f[S] = min(f[S], f[S - i] + a[i]) (i ∈ S)
运算重载一下即可。
*/
#include<cstdio>
#include<iostream>
int n,m,w;
int a[];
struct node{
int cnt,v;
node operator + (const int b)const{
node res;
if(v+b<=w)res.cnt=cnt,res.v=v+b;
else res.cnt=cnt+,res.v=b;
return res;
}
bool operator < (const node b)const{
if(cnt==b.cnt)return v<b.v;
return cnt<b.cnt;
}
}f[<<];
node min(node x,node y){
if(x<y)return x;
return y;
}
node make_node(int x,int y){
node res;
res.cnt=x;res.v=y;
return res;
}
int main(){
int sta;
scanf("%d%d",&n,&w);
m=(<<n)-;
for(int i=;i<=n;i++)scanf("%d",&a[i]);
for(sta=;sta<=m;sta++){
f[sta]=make_node(1e9,w);
for(int i=;i<=n;i++){
if(!((<<i-)&sta))continue;
f[sta]=min(f[sta],f[(<<i-)^sta]+a[i]);
}
}
printf("%d",f[m].cnt+);
return ;
}
100分 状压dp
洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper的更多相关文章
- 洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛 [迭代加深搜索]
题目传送门 摩天大楼里的奶牛 题目描述 A little known fact about Bessie and friends is that they love stair climbing ra ...
- P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 输入格式: Line 1: N and W separated by a spa ...
- P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp
这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大. 再单开一个g数组,存剩余空间就行了. 题干: 题目描述 A little k ...
- LUOGU P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- [USACO12MAR] 摩天大楼里的奶牛 Cows in a Skyscraper
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- [bzoj2621] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目链接 状压\(dp\) 根据套路,先设\(f[sta]\)为状态为\(sta\)时所用的最小分组数. 可以发现,这个状态不好转移,无法判断是否可以装下新的一个物品.于是再设一个状态\(g[sta] ...
- [luoguP3052] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper(DP)
传送门 输出被阉割了. 只输出最少分的组数即可. f 数组为结构体 f[S].cnt 表示集合 S 最少的分组数 f[S].v 表示集合 S 最少分组数下当前组所用的最少容量 f[S] = min(f ...
随机推荐
- hdu-5653 Bomber Man wants to bomb an Array.(区间dp)
题目链接: Bomber Man wants to bomb an Array. Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65 ...
- 批量处理JDBC语句提高处理速度
当需要成批插入或者更新记录时.可以采用Java的批量更新机制,这一机制允许多条语句一次性提交给数据库批量处理.通常情况下比单独提交处理更有效率 JDBC的批量处理语句包括下面两个方法: – ...
- 洛谷 P1187 3D模型
题目描述 一座城市建立在规则的n×m网格上,并且网格均由1×1正方形构成.在每个网格上都可以有一个建筑,建筑由若干个1×1×1的立方体搭建而成(也就是所有建筑的底部都在同一平面上的).几个典型的城市模 ...
- Mathf.Sin正弦
输入参数是弧度 Mathf.Sin public static float Sin(float f); Parameters Description Returns the sine of ang ...
- [转]JavaScript文件操作(2)-FileReader
在上篇文章中,我介绍了在JavaScript操作文件,重点讲了如何取得File对象. 这些对象包含的文件的元数据在上传或者拖放到浏览器中时可以获取到.有了文件当然接下来就是读取文件了. FileRea ...
- JUST第二界算法设计大赛题解
1.问题描述: 悠悠假期同叔叔一起去书店,他选中了六本书,每本书的单价(单位:元)分别为:3.1,1.7,2,5.3,0.9 和7.2.不巧的是,叔叔只带了十几块钱,为了让悠悠高兴,叔叔同意买书,但提 ...
- RabbitMQ队列,Redis\Memcached缓存
RabbitMQ RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统. MQ全称Message Queue,消息队列(MQ)是一种应用程序对应用程序的通信方式.应用程序通过读写出入队列 ...
- Redis的安装和配置文件
实验环境:Centos6.8 Redis版本:3.0.6 下载Redis,并放到/usr/local/soft下: yum -y install gcc automake autoconf libto ...
- java基础知识(9)---异常
异 常: 异常:就是不正常.程序在运行时出现的不正常情况.其实就是程序中出现的问题.这个问题按照面向对象思想进行描述,并封装成了对象.因为问题的产生有产生的原因.有问题的名称.有问题的描述等多个属性信 ...
- PWA PSI statusingclient.UpdateStatus更新任务页面的AssnCustomFields的TextValue值
1.注意Changesxml格式和下面一定要一样 2.CustomFieldGuid和CustomFieldName都不能少,自定义域的uid和name其中uid或者是MD_PROP_UID_SECO ...