动态规划(状态压缩):BZOJ 2621 [Usaco2012 Mar]Cows in a Skyscraper
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
5
6
3
7
Sample Output
#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的更多相关文章
- BZOJ2621 [Usaco2012 Mar]Cows in a Skyscraper
首先比较容易想到是状态压缩DP 令$f[S]$表示选取了集合$S$以后,已经送了最少次数$cnt$且当前电梯剩下的体积$rest$最大(即$f[S]$是一个二元组$(cnt, rest)$) 于是$f ...
- bzoj2621: [Usaco2012 Mar]Cows in a Skyscraper(状压DP)
第一眼是3^n*n的做法...然而并不可行T T 后来发现对于奶牛的一个状态i,最优情况下剩下那个可以装奶牛的电梯剩下的可用重量是一定的,于是我们设f[i]表示奶牛状态为i的最小电梯数,g[i]为奶牛 ...
- [动态规划]状态压缩DP小结
1.小技巧 枚举集合S的子集:for(int i = S; i > 0; i=(i-1)&S) 枚举包含S的集合:for(int i = S; i < (1<<n); ...
- [POJ 2923] Relocation (动态规划 状态压缩)
题目链接:http://poj.org/problem?id=2923 题目的大概意思是,有两辆车a和b,a车的最大承重为A,b车的最大承重为B.有n个家具需要从一个地方搬运到另一个地方,两辆车同时开 ...
- POJ 1185 炮兵阵地(动态规划+状态压缩)
炮兵阵地 Description 司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队.一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H" 表示),也可能是平原( ...
- ACM学习历程—HDU1584 蜘蛛牌(动态规划 && 状态压缩 || 区间DP)
Description 蜘蛛牌是windows xp操作系统自带的一款纸牌游戏,游戏规则是这样的:只能将牌拖到比她大一的牌上面(A最小,K最大),如果拖动的牌上有按顺序排好的牌时,那么这些牌也跟着一起 ...
- HDOJ-1074(动态规划+状态压缩)
Doing Homework HDOJ-1074 1.本题主要用的是状态压缩的方法,将每种状态用二进制压缩表示 2.状态转移方程:dp[i|(1<<j)]=min(dp[i|(1<& ...
- [ZOJ 3662] Math Magic (动态规划+状态压缩)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3662 之前写过这道题,结果被康神吐槽说代码写的挫. 的确,那时候 ...
- 动态规划状态压缩-poj1143
题目链接:http://poj.org/problem?id=1143 题目描述: 代码实现: #include <iostream> #include <string.h> ...
随机推荐
- 利用switch语句进行多选一判断。
<!doctype html> <meta http-equiv="content-type" content="text/html" cha ...
- Android Studio安装及首次运行遇到的问题
Android Studio,下载地址:http://developer.android.com/sdk/index.html.需要注意的是Android Studio需要JDK 1.7+才可以安装, ...
- mysql复习笔记
阅读目录 1.什么是SQL语句2.使用sql语句创建数据库和表3.创建数据表4.数据完整性约束5.四中基本字符类型说明6.SQL基本语句7.类型转换函数8.日期函数9.数学函数10.字符串函数11.联 ...
- Win10安卓模拟器Visual Studio Emulator for Android使用简介(转)
Visual Studio Emulator for Android是微软官方发布的独立版本的安卓模拟器,这款软件可以让安卓应用开发者更加轻松的用Visual Studio编写Android应用,据说 ...
- HTML5 文件域+FileReader 分段读取文件并上传(八)-WebSocket
一.同时上传多个文件处理 HTML: <div class="container"> <div class="panel panel-default&q ...
- C# Activator.CreateInstance()
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- iOS远程消息推送自我整理版
@interface AppDelegate () <UIApplicationDelegate> @end @implementation AppDelegate - (BOOL)app ...
- JS键盘码值表
值得注意的是,keypress事件中获取的keycode.which,都是按键对应的ascii值,而不完全对应下面的列表. 将字符转换为ascii值可以用charCodeAt函数. keycode 8 ...
- helloServlet
创建第一个web程序 用myeclipse创建一个web项目,继而创建一个servlet 自动帮助你创建了一个web.xml文件 <servlet>....<servlet>指 ...
- Bootstrap_Javascript_图片轮播
一 . 结构分析 一个轮播图片主要包括三个部分: ☑ 轮播的图片 ☑ 轮播图片的计数器 ☑ 轮播图片的控制器 第一步:设计轮播图片的容器.在 Bootstrap 框架中采用 carousel 样式,并 ...