洛谷 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>
#include<algorithm>
using namespace std; int n,w,flag,v[],bag[]; bool cmp(int x,int y){
return x>y;
} void dfs(int deep,int now){
if(flag)return;
if(now==n+){
flag=true;
return;
}
for(int i=;i<=min(deep,now);i++){
if(bag[i]+v[now]<=w){
bag[i]+=v[now];
dfs(deep,now+);
bag[i]-=v[now];
}
}
} int main(){
scanf("%d%d",&n,&w);
for(int i=;i<=n;i++)scanf("%d",&v[i]);
sort(v+,v+n+,cmp);
for(int deep=;deep;deep++){
dfs(deep,);
if(flag){
printf("%d\n",deep);
return ;
}
}
return ;
}
洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper的更多相关文章
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- 洛谷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 ...
随机推荐
- mysql 7.5.8 服务无法启动 服务没有报告任何错误
你安装的mysql位置不是c盘的话应该出现此问题. 1.打开bin下的mysql_config.pl文件,查找以下几行,把目录改成你安装mysql的目录即可. my $ldata = 'C:/Prog ...
- Hibernate关联关系的CRUD
本文以Group和User(一对多.多对一)双向关联为例,介绍关联关系的CRUD 下面先介绍两个属性 cascade:只影响CRUD中的CUD,即存储(save).更新(update).删除(de ...
- Python自然语言处理 - 系列三
有监督分类过程 ![enter image description here][1]例子:涉及一个特征器,给定一个姓名分析出是男性名字还是女性名字 分析:男性和女性的名字有一些鲜明的特点.以a,e 和 ...
- Python编程-多进程二
7.进程间通信(IPC)方式二:管道 (1)创建管道的类: Pipe([duplex]):在进程之间创建一条管道,并返回元组(conn1,conn2),其中conn1,conn2表示管道两端的连接对象 ...
- jQuery图片下滑切换焦点图
在线演示 本地下载
- scp 和 pscp
今天在做项目遇到了两个问题,做一些总结. 1.在项目中,防火墙的的相关配置是关于cisco企业级防火墙的配置,并不是window防火墙. 2. 在Linux上, scp linux文件 ...
- Raspberry 2B && Ubuntu mate 16.04 && *** 完美透明代理
Raspberry 2B && Ubuntu mate 16.04 && *** 完美透明代理 关键词:Raspberry 2B, Ubuntu mate 16.04 ...
- NSFetchedResultController与UITableView
1 #import "AppDelegate.h" #import "Book.h" @interface AppDelegate () @end @imple ...
- windows蓝屏错误小全
作者:siyizhu 日期:2005-11-27 字体大小: 小 中 大 引用内容 0 0x00000000 作业完成. 1 0x00000001 不正确的函数. 2 0x00000002 系统 ...
- SQL Server获取数据库的当前连接状态
SELECT * FROM [Master].[dbo].[SYSPROCESSES] WHERE [DBID]=(SELECT [DBID] FROM [Master].[dbo].[SYSDATA ...