洛谷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夸下海口:这么 ...
随机推荐
- [DeeplearningAI笔记]序列模型2.7负采样Negative sampling
5.2自然语言处理 觉得有用的话,欢迎一起讨论相互学习~Follow Me 2.7 负采样 Negative sampling Mikolov T, Sutskever I, Chen K, et a ...
- OpenCV---图像梯度
图像梯度 推文:[OpenCV入门教程之十二]OpenCV边缘检测:Canny算子,Sobel算子,Laplace算子,Scharr滤波器合辑 图像梯度可以把图像看成二维离散函数,图像梯度其实就是这个 ...
- JS中的匿名函数自执行、函数声明与函数表达式
先看一段jQuery源码中匿名函数自执行的例子: (function( window, undefined ) { // jquery code })(window); 另外一种常见的写法: +fun ...
- Mybatis批量删除之Error code 1064, SQL state 42000;
(一)小小的一次记载. (二):最近的项目都是使用MyBatis,批量新增自己都会写了,但是一次批量删除可把我给折腾了下,写法网上都有,但是照着做就是不行,最后问公司的人,问网友才得到答案,那就是jd ...
- File System Implementation 文件系统设计实现
先来扯淡吧,上一篇文章说到要补习的第二篇文章介绍文件系统的,现在就来写吧.其实这些技术都已经是很久以前的了,但是不管怎么样,是基础,慢慢来学习吧.有种直接上Spark源码的冲动.. 1. 这篇博客具体 ...
- 【CodeForces】915 F. Imbalance Value of a Tree 并查集
[题目]F. Imbalance Value of a Tree [题意]给定n个点的带点权树,求所有路径极差的和.n,ai<=10^6 [算法]并查集 [题解]先计算最大值的和,按点权从小到大 ...
- python学习笔记(十一)之序列
之前学习的列表,元组,字符串都是序列类型,有很多共同特点: 通过索引得到每一个元素,索引从0开始 通过分片的方法得到一个范围的元素的集合 很多通用的操作符(重复操作符,拼接操作符,成员关系操作符) 序 ...
- MQTT协议及推送服务(二)
MQTT简介 MQTT全称叫做Message Queuing Telemetry Transport,意为消息队列遥测传输,是IBM开发的一个即时通讯协议.由于其维护一个长连接以轻量级低消耗著称,所以 ...
- Metasploit 进阶
本文是"T00LS Metasploit(第二季)"的文档版,是个人在观看视频动手操作的一个记录,仅供学习.文中会介绍Metasploit的一些基本使用:主要包括远程代码执行.MI ...
- 工作中常用的Git操作--------(一)
今天主要记录一下平常工作当中使用的git操作: 1.git的安装这里省略: 2.git的操作指令: 在项目开发中,经常是拉去经理已经搭建好的一个项目,也就是给我们一个git地址.比如:http://g ...