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. Python之路【第十篇】:HTML -暂无等待更新

    Python之路[第十篇]:HTML -暂无等待更新

  2. Quartz Features

    Runtime Environments Quartz can run embedded within another free standing application Quartz can be ...

  3. 开源的Android开发框架-------PowerFramework使用心得(四)数据库管理DBFarmer

    DBFarmer是PowerFramework数据库管理工具的集合. 可以进行对象的存储,添加了setter和getter的参数会被收录到数据库中,每个参数作为一个项,int类型的id或_id会被作为 ...

  4. Android ListView 嵌套 ImageView,如何响应ImageView的点击和长按事件

    http://www.tuicool.com/articles/EZv2Uv 1.先说下嵌套在ListView中的ImageView如何响应点击事件 方法:在imageView中设置onClick属性 ...

  5. apache commons io包基本功能

    1. http://jackyrong.iteye.com/blog/2153812 2. http://www.javacodegeeks.com/2014/10/apache-commons-io ...

  6. eclipse中更改默认编码格式

    更改过程如下: (1)window->preferences->general->content Types, 选中java class file修改default encoding ...

  7. Topas命令详解

    Topas命令详解 执行topas命令后如图所示: #topas 操作系统的最全面动态,而又查看方便的性能视图就是topas命令了,下面以topas输出为例,对AIX系统的性能监控做简要描述,供运维工 ...

  8. angular.js 数字

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...

  9. 启动scala的方法

    1.从官网 http://www.scala-lang.org/download/ 下载scala二进制通用版本以后,在终端命令行添加下载解压包的bin目录到环境变量: export PATH=/Us ...

  10. 【CF39E】【博弈论】What Has Dirichlet Got to Do with That?

    Description You all know the Dirichlet principle, the point of which is that if n boxes have no less ...