洛谷题目链接:[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. 阿里校招内推C++岗位编程题第一题 空格最少的字符串

    给定一个字符串S和有效单词的字典D,请确定可以插入到S中的最小空格数,使得最终的字符串完全由D中的有效单词组成.并输出解. 如果没有解则应该输出n/a 例如: 输入: S = “ilikealibab ...

  2. Fluent Python: @property

    Fluent Python 9.6节讲到hashable Class, 为了使Vector2d类可散列,有以下条件: (1)实现__hash__方法 (2)实现__eq__方法 (3)让Vector2 ...

  3. Daily Scrum 9

    今天我们的开会内容有一下部分: Part 1:讨论当前遇到的困难 Part 2:明天的任务分工 ◆Part 1 当前的困难 由于之前我们得到的学长的文件并不完整,导致我们无法打开,在和老师进行积极沟通 ...

  4. BluetoothAdapter解析

    这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每个常量含义. 一 BluetoothAdapter简介 1.继承关系 该类仅继承了Object类; 2.该类作用 ...

  5. pycharm/webstorm创建react项目

    1.安装nodejs 2.安装reactapp依赖:npm install -g create-react-app 在pycharm/webstorm中选择react

  6. QT分析之QPushButton的初始化

    原文地址:http://blog.163.com/net_worm/blog/static/127702419201001003326522/ 在简单的QT程序的第二行,声明了一个QPushButto ...

  7. linux后台运行之screen和nohup

    3.1 nohup命令 如果你正在运行一个进程,而且你觉得在退出帐户时该进程还不会结束,那么可以使用nohup命令. 该命令可以在你退出帐户/关闭终端之后继续运行相应的进程. nohup就是不挂起的意 ...

  8. JVM(一)运行机制

    1.启动流程 2.JVM基本结构 PC寄存器 >每个线程拥有一个PC寄存器 >在线程创建时创建 >指向下一条指令的地址 >执行本地方法时,PC的值为undefined 方法区 ...

  9. 进程&线程间通信方式总结

    一.进程间的通信方式 # 管道( pipe ):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用.进程的亲缘关系通常是指父子进程关系. # 有名管道 (namedpip ...

  10. SpringBoot2.0(一) mybatis

    使用mybatis springboot使用mybatis主要依赖 mybatis-spring-boot-starter 来实现.其提供了2中解决方案,一种是使用注解:另一种是简化后的传统的xml方 ...