洛谷P3052 [USACO12MAR]摩天大楼里的奶牛 [迭代加深搜索]
摩天大楼里的奶牛
题目描述
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.
输出格式:
* A single integer, R, indicating the minimum number of elevator rides needed.
one of the R trips down the elevator.
输入输出样例
4 10
5
6
3
7
3
说明
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.
分析:
因为$n$的范围很小,所以可以想到迭代加深搜索(当然状压DP也可以,但是蒟蒻DP太菜了。。),枚举需要的分组数,然后直接dfs,只要能丢进去就丢进去,直到搜到底,然后回溯重复,知道找到最优结果。
Code:
//It is made by HolseLee on 24th July 2018
//Luogu.org P3052
#include<bits/stdc++.h>
using namespace std; int n,w,a[],e[]; inline bool dfs(int x,int tot)
{
for(int i=;i<=min(x,tot);i++){
if(a[x]+e[i]<=w){
e[i]+=a[x];
if(x==n)return ;
if(dfs(x+,tot))return ;
e[i]-=a[x];
}
}
return ;
} int main()
{
ios::sync_with_stdio(false);
cin>>n>>w;
for(int i=;i<=n;i++)cin>>a[i];
for(int i=;i<=n;i++){
if(dfs(,i)){
printf("%d\n",i);
break;
}
}
return ;
}
洛谷P3052 [USACO12MAR]摩天大楼里的奶牛 [迭代加深搜索]的更多相关文章
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- 洛谷 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]摩天大楼里的奶牛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 ...
- P3052 [USACO12MAR]摩天大楼里的奶牛(迭代加深搜索)
(已经一句话了) 第一反应:暴力 第二反应:朴素算法过不去 第三反应:没法折半暴搜(没法统计答案) 所以,歪歪了一个类似贪心刷表的方法,过了这道题. 首先,如果爆搜的话会有几个状态: 当前牛 当前几个 ...
- 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 ...
- [USACO12MAR]摩天大楼里的奶牛(状态压缩DP)
题意 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 题解 一看以为是弱智题.(可能真的是,我太菜了) 然后跟walthou夸下海口:这么 ...
随机推荐
- OpenCV---超大图像二值化和空白区域过滤
超大图像的二值化方法 1.可以采用分块方法, 2.先缩放处理就行二值化,然后还原大小 一:分块处理超大图像的二值化问题 def big_image_binary(image): print(image ...
- kvm虚拟机
###查看虚拟机的状态 [root@fgeserver2 ~]# virsh list --all Id Name State------------------------------------- ...
- 图:centrality
[定义]Centrality:图中每个节点v的相对重要度c(v),重要度是什么可根据具体应用定义. [估计方法] Degree centrality Betweenness centrality Cl ...
- 【Hadoop】大数据时代,我们为什么使用hadoop
博客已转移,请借一步说话.http://www.daniubiji.cn/archives/538 我们先来看看大数据时代, 什么叫大数据,“大”,说的并不仅是数据的“多”!不能用数据到了多少TB , ...
- js常见易错点
原文:http://www.jianshu.com/p/1c77853d4f01 前言 本文是我学习JavaScript过程中收集与整理的一些易错知识点,将分别从变量作用域,类型比较,this指向,函 ...
- javac -cp java -cp
///////////////////////////////////////////////////////////////////////////////////// 编译java文件的命令都知道 ...
- 蓝色的cms企业记账管理后台模板源码——后台
链接:http://pan.baidu.com/s/1bpxKGBP 密码:suda
- 挂载cifs报错mount error(13): Permission denied(域账号访问时报错)
Linux挂载Windows共享时,报以下错误: mount error(13): Permission deniedRefer to the mount.cifs(8) manual page (e ...
- C基础 time.h 简单思路扩展
前言 - time 简单需求 时间业务相关代码. 基本属于框架的最底层. 涉及的变动都很小. 以前参与游戏研发时候, 这方面需求不少, 各种被策划花式吊打. 转行开发互联网服务之后很少遇到这方面需求. ...
- [转载]FFmpeg完美入门[3] - FFmpeg功能及使用说明
1 ffplay对多媒体的支持能力验证 一.视频3gp 177X144 支持播放,在windows下播放正常,但是在linux下面偶有BUG 如果发现画面无法显示而声音可以播放的情况下可以试着切换全屏 ...