洛谷题目链接:[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的更多相关文章

  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. str和repr

    在Python2.6和Python3.0以及更早的版本中,在交互式模式下的输出本质上是使用repr,因此对于一些浮点数运算,会显示很多位: 4 / 5.0 #0.8000000000000004 但是 ...

  2. 共享程序集GAC

    原文标题: 原文地址:https://www.cnblogs.com/1996V/p/9037603.html 共享程序集GAC 我上面说了这么多有关CLR加载程序集的细节和规则,事实上,类似于msc ...

  3. eg_3

    3. 编写一个程序,返回一个 double 类型的二维数组,数组中的元素通过解析字符串参数获得,如字符串参数:“1,2;3,4,5;6,7,8”,则对应的数组为: d[0][0]=1.0, d[0][ ...

  4. 【转载】【翻译】Breaking things is easy///机器学习中安全与隐私问题(对抗性攻击)

    原文:Breaking things is easy 译文:机器学习中安全与隐私问题(对抗性攻击) 我是通过Infaraway的那篇博文才发现cleverhans-blog的博客的,这是一个很有意思的 ...

  5. lintcode-158-两个字符串是变位词

    158-两个字符串是变位词 写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 说明 What is Anagram? Two strings are ...

  6. phpshell提权

    实际操作中可以在webshell用udf.dll提权,用函数的上传文件功能上传文件到启动目录,再用shut函数重起系统.(目前没成功过,有 机会本地测试一下,先记录在这了).如果是英文版的系统,启动目 ...

  7. SQL SERVER技术内幕之4 子查询

    最外层查询的结果集会返回给调用者,称为外部查询.内部查询的结果是供外部查询使用的,也称为子查询.子查询可以分成独立子查询和相关子查询两类.独立子查询不依赖于它所属的外部查询,而相关子查询则须依赖它所属 ...

  8. delphi鼠标状态

    Screen.Cursor := crNo;

  9. 【Python】python动态类型

    在python中,省去了变量声明的过程,在引用变量时,往往一个简单的赋值语句就同时完成了,声明变量类型,变量定义和关联的过程,那么python的变量到底是怎样完成定义的呢? 动态类型 python使用 ...

  10. 【题解】洛谷P3709大爷的字符串题

    最近想要练习一下莫队(实在是掌握的太不熟练了啊.)这题一开始看到有点懵(题面杀),后来发现是要求众数的个数.乍一看好像很难的样子. 但仔细分析一下:首先往序列当中加入一个数,这个是很简单的,只需要维护 ...