洛谷题目链接:[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)])\)(在需要多用一个袋子的时候).

看代码理解一下吧.

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int N=18+5;
  4. int n, m, a[N];
  5. int f[(1<<20)], used[(1<<20)];
  6. int main(){
  7. //freopen("data.in", "r", stdin);
  8. ios::sync_with_stdio(false);
  9. cin >> n >> m;
  10. for(int i=0;i<n;i++) cin >> a[i];
  11. int U = (1<<n)-1;//U表示全集
  12. memset(f, 127, sizeof(f)); f[0] = 0;
  13. for(int i=0;i<=U;i++){
  14. for(int j=0;j<n;j++){
  15. if(i&(1<<j)) continue;
  16. if(!used[i]){//袋子内没有物品时放物品需要多用一个袋子
  17. if(f[i|(1<<j)] > f[i]+1) f[i|(1<<j)] = f[i]+1, used[i|(1<<j)] = a[j];
  18. else if(f[i|(1<<j)] == f[i]+1) used[i|(1<<j)] = min(used[i|(1<<j)], used[i]+a[j]);
  19. } else {
  20. if(used[i]+a[j] <= m){
  21. if(f[i|(1<<j)] > f[i]) f[i|(1<<j)] = f[i], used[i|(1<<j)] = used[i]+a[j];
  22. else if(f[i|(1<<j)] == f[i]) used[i|(1<<j)] = min(used[i|(1<<j)], used[i]+a[j]);
  23. }
  24. else {
  25. if(f[i|(1<<j)] > f[i]+1) f[i|(1<<j)] = f[i]+1, used[i|(1<<j)] = a[j];
  26. else if(f[i|(1<<j)] == f[i]+1) used[i|(1<<j)] = min(used[i|(1<<j)], used[i]+a[j]);
  27. }
  28. }
  29. }
  30. }
  31. cout << f[U] << endl;
  32. return 0;
  33. }

[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper的更多相关文章

  1. 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...

  2. [USACO12MAR] 摩天大楼里的奶牛 Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  3. P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 给出n个物品,体积为w[i],现把其分成若干组,要求每组总体积<=W,问最小分组.(n<=18) 输入格式: Line 1: N and W separated by a spa ...

  4. 洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...

  5. P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp

    这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大. 再单开一个g数组,存剩余空间就行了. 题干: 题目描述 A little k ...

  6. 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 ...

  7. [bzoj2621] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper

    题目链接 状压\(dp\) 根据套路,先设\(f[sta]\)为状态为\(sta\)时所用的最小分组数. 可以发现,这个状态不好转移,无法判断是否可以装下新的一个物品.于是再设一个状态\(g[sta] ...

  8. [luoguP3052] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper(DP)

    传送门 输出被阉割了. 只输出最少分的组数即可. f 数组为结构体 f[S].cnt 表示集合 S 最少的分组数 f[S].v 表示集合 S 最少分组数下当前组所用的最少容量 f[S] = min(f ...

  9. [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper (状态压缩DP)

    不打算把题目放着,给个空间传送门,读者们自己去看,传送门(点我)    . 这题是自己做的第一道状态压缩的动态规划. 思路: 在这题中,我们设f[i]为i在二进制下表示的那些牛所用的最小电梯数. 设g ...

随机推荐

  1. 基础数据类型-list

    序列是python中的基础数据结构,序列里每一个元素都有一个下标,从0开始,依次递增. list,tuple,dictionary是使用最频繁的三类数据结构. (1)序列都有的方法包括:索引,切片,检 ...

  2. 一次大量TIME_WAIT和Recv-Q 堵塞问题排查思路

    记录一下周末出现问题~     仅自己摘记不做任何参考. 第一天故障: 现象: 公司销售群和售后群炸了,说老后台(1.0版本)崩溃了,因为还有部门的业务没来得及迁移到新后台,我当时正在打农药哈哈~ 后 ...

  3. 算法与数据结构3.3 calculator

    ★实验任务 小 V 发明了一个神奇的整数计算器: 给定一个合法的表达式,这个计算器能求出这个表达式的最终答案. 表达式可能包含: +:运算符,整数加法.如 1+1=2 -:运算符,整数减法.如 1-1 ...

  4. C++课堂作业2016.05.04

    GitHub/object-oriented 作业题目 开课后的第一次作业,简单地写了一个类,用成员函数来实现计算圆的面积. [代码] main.cpp #include "Area.h&q ...

  5. ACM 第七天

    水题 B - Minimum’s Revenge There is a graph of n vertices which are indexed from 1 to n. For any pair ...

  6. 【Docker 命令】- images命令

    docker images : 列出本地镜像. 语法 docker images [OPTIONS] [REPOSITORY[:TAG]] OPTIONS说明: -a :列出本地所有的镜像(含中间映像 ...

  7. window.navigator.standalone 检测iOS WebApp是否运行在全屏模式

    iOS上的Safari浏览器可以让Web应用程序全屏显示,以取得类似本地应用的显示效果.但是这需要用户把Web应用程序的图标添加到主屏幕才可以.作为开发者,为了更好的显示效果,我们可能希望自己开发的W ...

  8. 【Asp.Net】IIS应用程序池添加ASP.NET v4.0

    可能在安装.NET Framewrok 4.0之前,IIS就已经装好了,结果在IIS的应用程序池中只有.NET 2.0的Classic .NET AppPool和DefaultAppPool.在使用v ...

  9. AngularJS注入依赖路由总结

    属性 描述 $dirty  表单有填写记录 $valid 字段内容是合法的 $invalid 字段内容是非法的 $pristine 表单没有填写记录 什么事依赖注入? 依赖注入是一种软件设计模式,在这 ...

  10. BZOJ4771 七彩树(dfs序+树上差分+主席树)

    考虑没有深度限制怎么做.显然的做法是直接转成dfs序上主席树,但如果拓展到二维变成矩形数颜色数肯定没法做到一个log. 另一种做法是利用树上差分.对于同种颜色的点,在每个点处+1,dfs序相邻点的lc ...