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 ...
随机推荐
- js统计图表插件 Echarts
Echarts 用于制作数据统计图表,一个纯 Javascript 的图表库,快捷简便的生成统计图表. 官网:https://www.echartsjs.com/ 效果 html <!DOCTY ...
- linux shell脚本学习笔记一
一.文件比较运算符-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]-d filename 如果 filename为目录,则为真 [ -d /tm ...
- php第二十九节课
文件 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...
- MySQL-date和datetime
MySQL中 date表示只有日期: insert into stu values(id = null, birthday = '2000-01-11'); datetime则还包含了时间: inse ...
- FileOutputStream将从一个文件中读取的内容写到另一个文件中
package com.janson.day2018082 import java.io.FileInputStream; import java.io.FileNotFoundException; ...
- CentOS 6 Yum本地源配置
#cd /etc/yum.repos.d #rm CentOS-Base.repo CentOS-Base.repo 是yum 网络源的配置文件(默认) #vi CentOS-Media.repo C ...
- mysql连接错误解决(ERROR 2049 (HY000): Connection using old (pre-4.1.1) authentication protocol ref used (client option 'secure_auth' enabled))
当使用mysql的新版本是,连接老版本的mysql,就会有可能报: ERROR 2049 (HY000): Connection using old (pre-4.1.1) authenticatio ...
- JavaScript 面向对象的编程(二) 类的封装
类的定义 方式一 var Book = function(id, name, price){ //私有属性,外部不能直接访问 var num = 1; //私有方法, function checkId ...
- 关于git上传GitHub以及码云(gitee)
如果你是gitee(码云),点击链接跳转 首先,你的有一个GitHub的账号(然后新建项目我就不说了) # Linux的方法 GitHub网站下的,点击settings下的emails,确认自己的邮箱 ...
- [luoguP2782] 友好城市(DP)
传送门 转化成 lis 后 n2 搞就行 ——代码 #include <cstdio> #include <iostream> #include <algorithm&g ...