2621: [Usaco2012 Mar]Cows in a Skyscraper

Time Limit: 20 Sec  Memory Limit: 128 MB
Submit: 303  Solved: 150
[Submit][Status][Discuss]

Description

[Mark Gordon, Neal Wu, Fatih Gelgi, 2012] 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.

Input

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.

Output

* Line 1: A single integer, R, indicating the minimum number of elevator rides needed. * Lines 2..1+R: Each line describes the set of cows taking one of the R trips down the elevator. Each line starts with an integer giving the number of cows in the set, followed by the indices of the individual cows in the set.

Sample Input

4 10
5
6
3
7

Sample Output

3
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int INF=;
const int maxn=<<;
int n,W,v[];
struct Node{
int x,y;
Node(int x_=INF,int y_=INF){
x=x_;y=y_;
}
Node operator +(Node b){
if(y+b.y>W)
return Node(x+b.x+,b.y);
return Node(x+b.x,y+b.y);
}
bool operator <(const Node &b)const{
return x!=b.x?x<b.x:y<b.y;
}
};
Node f[maxn];
int cnt,st[maxn];
int st2[maxn],tmp;
int vis[maxn],tim;
int main(){
scanf("%d%d",&n,&W);
for(int i=;i<=n;i++)
scanf("%d",&v[i]);
st[++cnt]=;f[].x=f[].y=;
for(int Ti=;Ti<=n;Ti++){
tim++;
for(int j=;j<=cnt;j++){
for(int i=;i<=n;i++){
if(st[j]&(<<(i-)))
continue;
f[st[j]|(<<(i-))]=min(f[st[j]|(<<(i-))],f[st[j]]+Node(,v[i]));
if(vis[st[j]|(<<(i-))]!=tim){
st2[++tmp]=st[j]^(<<(i-));
vis[st[j]|(<<(i-))]=tim;
}
}
}
memcpy(st,st2,sizeof(st2));
cnt=tmp;tmp=;
}
printf("%d\n",f[(<<n)-].y>?+f[(<<n)-].x:f[(<<n)-].x);
return ;
}

动态规划(状态压缩):BZOJ 2621 [Usaco2012 Mar]Cows in a Skyscraper的更多相关文章

  1. BZOJ2621 [Usaco2012 Mar]Cows in a Skyscraper

    首先比较容易想到是状态压缩DP 令$f[S]$表示选取了集合$S$以后,已经送了最少次数$cnt$且当前电梯剩下的体积$rest$最大(即$f[S]$是一个二元组$(cnt, rest)$) 于是$f ...

  2. bzoj2621: [Usaco2012 Mar]Cows in a Skyscraper(状压DP)

    第一眼是3^n*n的做法...然而并不可行T T 后来发现对于奶牛的一个状态i,最优情况下剩下那个可以装奶牛的电梯剩下的可用重量是一定的,于是我们设f[i]表示奶牛状态为i的最小电梯数,g[i]为奶牛 ...

  3. [动态规划]状态压缩DP小结

     1.小技巧 枚举集合S的子集:for(int i = S; i > 0; i=(i-1)&S) 枚举包含S的集合:for(int i = S; i < (1<<n); ...

  4. [POJ 2923] Relocation (动态规划 状态压缩)

    题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...

  5. POJ 1185 炮兵阵地(动态规划+状态压缩)

    炮兵阵地 Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原( ...

  6. ACM学习历程—HDU1584 蜘蛛牌(动态规划 && 状态压缩 || 区间DP)

    Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么这些牌也跟着一起 ...

  7. HDOJ-1074(动态规划+状态压缩)

    Doing Homework HDOJ-1074 1.本题主要用的是状态压缩的方法,将每种状态用二进制压缩表示 2.状态转移方程:dp[i|(1<<j)]=min(dp[i|(1<& ...

  8. [ZOJ 3662] Math Magic (动态规划+状态压缩)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3662 之前写过这道题,结果被康神吐槽说代码写的挫. 的确,那时候 ...

  9. 动态规划状态压缩-poj1143

    题目链接:http://poj.org/problem?id=1143 题目描述: 代码实现: #include <iostream> #include <string.h> ...

随机推荐

  1. log4j中Spring控制台输出Debug级信息过多解决方法

    log4j中Spring控制台输出Debug级信息过多解决方法 >>>>>>>>>>>>>>>>> ...

  2. 前台之boostrap

    这个网址有些东西不错

  3. PAT_2-08. 用扑克牌计算24点

    一副扑克牌的每张牌表示一个数(J.Q.K分别表示11.12.13,两个司令都表示6).任取4张牌,即得到4个1~13的数,请添加运算符 (规定为加+ 减- 乘* 除/ 四种)使之成为一个运算式.每个数 ...

  4. Android Parcelable Trans byte[]

    思路: http://stackoverflow.com/questions/10898116/make-custom-parcelable-containing-byte-array 谢谢, 这位外 ...

  5. 玩转CSLA.NET小技巧系列二:使用WCF无法上传附件,提示413 Entity Too Large

    背景:由于系统需要展示图片,客户上传图片到本地客户端目录,然后在数据库中存储本地图片地址,和图片二进制数据 错误原因:我是使用CSLA的WCF服务,使用了数据门户,WCF协议使用的是wsHttpBin ...

  6. wpf 中DataGrid 控件的样式设置及使用

    本次要实现的效果为: 这个DataGrid需要绑定一个集合对象,所以要先定义一个Experience类,包含三个字段 /// <summary> /// 定义工作经历类 /// </ ...

  7. underscorejs-shuffle学习

    2.21 shuffle 2.21.1 语法 _.shuffle(list) 2.21.2 说明 返回一个随机乱序的list副本数组, 使用 Fisher-Yates shuffle 来进行随机乱序. ...

  8. 常见的mongodb可视化工具

    一.MongoVue   1.MongoVUE是一款比较好用的MongoDB客户端工具,可以为大家提供一个高度.简洁可用的MongoDB管理界面. 2.通过MongoVUE,用户可以用树形.表格及bj ...

  9. Fatal error: Class 'ZipArchive' not found的解决办法

    今天在Linux底下编写导出EXCEL文件并显示输出时,抛出“ZipArchive library is not enabled” 的异常.而我在本地的windows下的代码则是运行正常的. 原因是: ...

  10. PLSQL Developer如何设置自动打开上次编辑的文件

    作为开发人员经常把sql语句保存到文件中以方便下次继续使用,问题是plsqlDev重启后每次都需要手工打开这个文件,好不方便: 以下设置是plsqlDev启动后自动打开上次编辑的文件. 选择配置> ...