P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp
这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大.
再单开一个g数组,存剩余空间就行了.
题干:
题目描述 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 ( <= W <= ,,) pounds and cow i weighs C_i ( <= C_i <= W) pounds. Please help Bessie figure out how to get all the N ( <= N <= ) 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<=)
输入输出格式
输入格式: * Line : N and W separated by a space. * Lines ..+N: Line i+ 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. 输入输出样例
输入样例#: 复制 输出样例#: 复制 说明 There are four cows weighing , , , and pounds. The elevator has a maximum weight capacity of pounds. We can put the cow weighing 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 involves cow # and #, elevator ride involves cow #, and elevator ride involves cow #. Several other solutions are possible for this input.
代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(int i = a;i <= n;i++)
#define lv(i,a,n) for(int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = << ;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
int n,W;
int w[];
int g[ << ],f[ << ];
int main()
{
read(n);read(W);
duke(i,,n)
{
read(w[i]);
}
memset(f,,sizeof(f));
f[] = ;
g[] = W;
duke(i,,( << n) - )
{
duke(j,,n)
{
if(i & ( << (j - )))
continue;
if(g[i] >= w[j] && f[i | ( << (j - ))] >= f[i])
{
f[i | ( << (j - ))] = f[i];
g[i | ( << (j - ))] = max(g[i | ( << (j - ))],g[i] - w[j]);
}
else if(g[i] < w[j] && f[i | ( << (j - ))] >= f[i] + )
{
f[i | ( << (j - ))] = f[i] + ;
g[i | ( << (j - ))] = max(g[i | ( << (j - ))],W - w[j]);
}
}
}
printf("%d\n",f[( << n) - ]);
return ;
}
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp的更多相关文章
- 洛谷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
题目描述 给出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 ...
- 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 ...
- [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 ...
随机推荐
- TWaver MONO模板库新鲜出炉 精彩纷呈
MONO Design在线3D建模平台网站, www.mono-design.cn,开发组的成员们已经开始紧锣密鼓的对这个平台进行内测.在之前的文章里,我们提到用户可以获得多种多样的TWaver官方模 ...
- CF 429B B.Working out (四角dp)
题意: 两个人一个从左上角一个从左下角分别开始走分别走向右下角和右上角,(矩阵每个格子有数)问到达终点后可以得到的最大数是多少,并且条件是他们两个相遇的时候那个点的数不能算 思路: 首先这道题如果暴力 ...
- tarjan求强连通分量模板
什么是强连通分量? 百度百科 有向图强连通分量:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(stro ...
- Python-函数和代码复用
函数的定义与使用 >函数的理解与定义 函数是一段代码的表示 -函数是一段具有特定功能的.可重用的语句组 -函数是一种功能的抽象,一般函数表达特定功能 -两个作用:降低编程难度 和 代码复用 de ...
- Promise对象和回调函数,解决异步数据传递问题
下面的代码例子,均已小程序的异步请求数据为案例来说明 1.利用回调函数,来解决异步数据传递问题 异步操作api.js const getBooks = (url, callback) => { ...
- react入门----基础语法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- BZOJ 1832、1787 洛谷 4281 [AHOI2008]紧急集合
[题解] 题目要求找到一个集合点,使3个给定的点到这个集合点的距离和最小,输出集合点的编号以及距离. 设三个点为A,B,C:那么我们可以得到Dis=dep[A]+dep[B]+dep[C]-dep[L ...
- 【02】json语法
[02] JSON 语法是 JavaScript 语法的子集. JSON 语法规则 JSON 语法是 JavaScript 对象表示法语法的子集. 数据在名称/值对中 数据由逗号分隔 花括号保存对象 ...
- Garden of Eden
Garden of Eden Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others ...
- js的jsonp
window.ajaxJsonp=function(params) { params = params || {}; params.data = params.data || {}; var json ...