这个状压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的更多相关文章

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

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

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

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

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

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

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

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

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

  6. [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. 梦想CAD控件打印相关

    一.打印设置 在顶部快速访问工具栏单击打印按钮或者直接输入PLOT命令或者点击打印控制的打印设置按钮打开打印对话框.c#代码实现如下: //打印设置 private void Print1() {   ...

  2. 05Oracle Database 表空间查看,创建,修改及删除

    Oracle Database 表空间查看,创建,修改及删除 查看用户表空间 查看数据库管理员表空间表结构 desc dba_tablespaces; 查询表空间名称从管理员表空间表中 select ...

  3. python bs4库

    Beautiful Soup parses anything you give it, and does the tree traversal stuff for you. BeautifulSoup ...

  4. idea之快速查看类所在jar包

  5. 模板—AC自动机

    #include<iostream> #include<cstdio> #include<cstring> using namespace std; struct ...

  6. C++ std::map的安全遍历并删除元素的方法

    首先我们讲遍历std::map, 大部分人都能写出第一种遍历的方法,但这种遍历删除的方式并不太安全. 第一种 for循环变量: #include<map> #include<stri ...

  7. JavaScript--小白入门篇2

    一.布尔值和关系运算符.逻辑运算符 1.1 布尔值   我们上篇文章说了,学习了两种变量的类型数值型.字符串型.   实际上,还有很多变量的类型.我们今天再学习一种,叫做“布尔类型”. 数值型里面的值 ...

  8. Bet(The 2016 ACM-ICPC Asia China-Final Contest 思路题)

    题目: The Codejamon game is on fire! Fans across the world are predicting and betting on which team wi ...

  9. 作业 3-5 switch语句的应用

    /*输入五级制成绩(A-E),输出相应的百分制成绩(0-100)区间*/ #include<stdio.h> int main(void) { char ch;/*定义一个字符*/ pri ...

  10. centOS目录结构介绍

    Linux / CentOS目录结构 /: 根目录,一般根目录下只存放目录,不要存放文件,/etc./bin./dev./lib./sbin应该和根目录放置在一个分区中 /bin:/usr/bin: ...