[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
洛谷题目链接:[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.
输出格式:
- A single integer, R, indicating the minimum number of elevator rides needed.
one of the R trips down the elevator.
输入输出样例
输入样例#1:
4 10
5
6
3
7
输出样例#1:
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\)个物品,每个物品有一个大小\(C_i\),现在有若干个袋子来装这些物品,且这些物品的大小和不能超过袋子的大小,问要怎么样分组才能用尽量少的袋子装完这些物品.
题解: 首先看数据范围,发现\(n\)是非常小的,可以直接用一些比较暴力的方法过掉.这里我用的是状压DP.
我们用\(f[i]\)表示\(i\)状态下所需的袋子数(\(i\)为一个二进制数表示以选的物品),再用\(used[i]\)表示\(i\)状态袋子内的使用空间,用\(j\)枚举选用的物品.那么显然有状态转移方程:\(f[i|(1<<j)]=min(f[i]+1, f[i|(1<<j)])\)(在需要多用一个袋子的时候).
看代码理解一下吧.
#include<bits/stdc++.h>
using namespace std;
const int N=18+5;
int n, m, a[N];
int f[(1<<20)], used[(1<<20)];
int main(){
//freopen("data.in", "r", stdin);
ios::sync_with_stdio(false);
cin >> n >> m;
for(int i=0;i<n;i++) cin >> a[i];
int U = (1<<n)-1;//U表示全集
memset(f, 127, sizeof(f)); f[0] = 0;
for(int i=0;i<=U;i++){
for(int j=0;j<n;j++){
if(i&(1<<j)) continue;
if(!used[i]){//袋子内没有物品时放物品需要多用一个袋子
if(f[i|(1<<j)] > f[i]+1) f[i|(1<<j)] = f[i]+1, used[i|(1<<j)] = a[j];
else if(f[i|(1<<j)] == f[i]+1) used[i|(1<<j)] = min(used[i|(1<<j)], used[i]+a[j]);
} else {
if(used[i]+a[j] <= m){
if(f[i|(1<<j)] > f[i]) f[i|(1<<j)] = f[i], used[i|(1<<j)] = used[i]+a[j];
else if(f[i|(1<<j)] == f[i]) used[i|(1<<j)] = min(used[i|(1<<j)], used[i]+a[j]);
}
else {
if(f[i|(1<<j)] > f[i]+1) f[i|(1<<j)] = f[i]+1, used[i|(1<<j)] = a[j];
else if(f[i|(1<<j)] == f[i]+1) used[i|(1<<j)] = min(used[i|(1<<j)], used[i]+a[j]);
}
}
}
}
cout << f[U] << endl;
return 0;
}
[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 ...
- [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
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- 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 ...
- [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 ...
- [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper (状态压缩DP)
不打算把题目放着,给个空间传送门,读者们自己去看,传送门(点我) . 这题是自己做的第一道状态压缩的动态规划. 思路: 在这题中,我们设f[i]为i在二进制下表示的那些牛所用的最小电梯数. 设g ...
随机推荐
- Python3 循环表达式
一 While循环 基本循环 while 条件: 执行内容 #循环体 ... #循环体 ... #循环体 # 若条件为真,执行循环体内容 # 若条件为假,不执行循环体内容 实例1(Python 3.0 ...
- 2019寒假训练营寒假作业(三) MOOC的网络空间安全概论笔记部分
目录 第五章 网络攻防技术 5.1:网络信息收集技术--网络踩点 信息收集的必要性及内容 网络信息收集技术 网络踩点(Footprinting) 网络踩点常用手段 5.2:网络信息收集技术 --网络扫 ...
- caffe2安装教程
相比于网上的安装教程不如直接看官方安装教程:https://caffe2.ai/docs/getting-started.html?platform=windows&configuration ...
- 【Redis】- 总结精讲
本文围绕以下几点进行阐述 1.为什么使用redis2.使用redis有什么缺点3.单线程的redis为什么这么快4.redis的数据类型,以及每种数据类型的使用场景5.redis的过期策略以及内存淘汰 ...
- Python的7种性能测试工具:timeit、profile、cProfile、line_profiler、memory_profiler、PyCharm图形化性能测试工具、objgraph
1.timeit: >>> import timeit >>> def fun(): ): a = i * i >>> timeit.timeit ...
- windows下apache+php安装
1.安装apache 通过exe安装,如果80端口被占用,修改httpd.conf中的Listen,然后再次用exe安装,选择repaire 2.安装php 解压php包,添加系统变量 path,加上 ...
- Python对字符串进行MD5加密处理
import hashlibimport sysreload(sys)sys.setdefaultencoding('utf-8') m = hashlib.md5()m.update('123456 ...
- BZOJ 1911 特别行动队(斜率优化DP)
应该可以看出这是个很normal的斜率优化式子.推出公式搞一搞即可. # include <cstdio> # include <cstring> # include < ...
- 【bzoj5110】[CodePlus2017]Yazid 的新生舞会 Treap
题目描述 求一个序列所有的子区间,满足区间众数的出现次数大于区间长度的一半. 输入 第一行2个用空格隔开的非负整数n,type,表示序列的长度和数据类型.数据类型的作用将在子任务中说明. 第二行n个用 ...
- robot framework连接Oracle错误:ORA-12504: TNS:listener was not given the SERVICE_NAME in CONNECT_DATA
在使用robot framework的关键字Connect to Database Using Custom params连接Oracle数据库: Connect to Database Using ...