BZOJ2621 [Usaco2012 Mar]Cows in a Skyscraper
首先比较容易想到是状态压缩DP
令$f[S]$表示选取了集合$S$以后,已经送了最少次数$cnt$且当前电梯剩下的体积$rest$最大(即$f[S]$是一个二元组$(cnt, rest)$)
于是$f[S] = min_{i \in S} f[S - {i}] + v[i]$
重载的$<$和$+$运算详情就请看程序好了,反正就是一个贪心思想,总复杂度$O(n * 2 ^ {n - 1})$
/**************************************************************
Problem: 2621
User: rausen
Language: C++
Result: Accepted
Time:532 ms
Memory:13092 kb
****************************************************************/ #include <cstdio>
#include <algorithm> using namespace std;
const int N = ;
const int S = << N;
const int inf = 1e9; int n, mx, mxs;
int a[S]; struct data {
int cnt, rest;
data(int _c = , int _r = mx) : cnt(_c), rest(_r) {} inline data operator + (int t) const {
static data res;
res = *this;
res.rest -= t;
if (res.rest < ) ++res.cnt, res.rest += mx;
return res;
} inline bool operator < (const data &d) const {
return cnt == d.cnt ? rest < d.rest : cnt < d.cnt;
}
} f[S]; int main() {
int i, s, t, now;
scanf("%d%d", &n, &mx);
mxs = ( << n) - ;
for (i = ; i <= n; ++i) scanf("%d", &a[ << i - ]);
f[] = data(, mx);
for (s = ; s <= mxs; ++s) {
t = s, f[s] = data(inf, mx);
while (t) {
now = t & (-t);
f[s] = min(f[s], f[s ^ now] + a[now]);
t -= now;
}
}
printf("%d\n", f[mxs].cnt + (f[mxs].rest != mx));
return ;
}
BZOJ2621 [Usaco2012 Mar]Cows in a Skyscraper的更多相关文章
- bzoj2621: [Usaco2012 Mar]Cows in a Skyscraper(状压DP)
第一眼是3^n*n的做法...然而并不可行T T 后来发现对于奶牛的一个状态i,最优情况下剩下那个可以装奶牛的电梯剩下的可用重量是一定的,于是我们设f[i]表示奶牛状态为i的最小电梯数,g[i]为奶牛 ...
- 动态规划(状态压缩):BZOJ 2621 [Usaco2012 Mar]Cows in a Skyscraper
2621: [Usaco2012 Mar]Cows in a Skyscraper Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 303 Sol ...
- 2620: [Usaco2012 Mar]Haybale Restacking
2620: [Usaco2012 Mar]Haybale Restacking Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 201 Solved: ...
- [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
洛谷题目链接:[USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- 洛谷P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 题目描述 A little known fact about Bessie and friends is ...
- [bzoj2621] [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
题目链接 状压\(dp\) 根据套路,先设\(f[sta]\)为状态为\(sta\)时所用的最小分组数. 可以发现,这个状态不好转移,无法判断是否可以装下新的一个物品.于是再设一个状态\(g[sta] ...
- 洛谷 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
题目描述 A little known fact about Bessie and friends is that they love stair climbing races. A better k ...
- P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper 状压dp
这个状压dp其实很明显,n < 18写在前面了当然是状压.状态其实也很好想,但是有点问题,就是如何判断空间是否够大. 再单开一个g数组,存剩余空间就行了. 题干: 题目描述 A little k ...
随机推荐
- JS学习笔记(三) 对象
参考资料: 1. http://www.w3school.com.cn/js/js_objects.asp ☂ 知识点: ☞ Javascript中的所有事物都是对象. ☞ Javascript是基于 ...
- iOS - Swift Dictionary 字典
前言 public struct Dictionary<Key : Hashable, Value> : CollectionType, DictionaryLiteralConverti ...
- iOS - ImageCache 网络图片缓存
1.ImageCache 使用内存缓存方式: 使用沙盒缓存方式: 使用网络图片第三方库方式: SDWebImage: iOS 中著名的网络图片处理框架 包含的功能:图片下载.图片缓存.下载进度监听.g ...
- python使用open经常报错:TypeError: an integer is required的解决方案
错误是由于从os模块引入了所有的函数导致的,os模块下有一个open函数,接受整型的文件描述符和打开模式,from os import *引入os模块的open函数,覆盖了python内建的open函 ...
- DHCP 工作原理
DHCP 详解 DHCP 工作原理 一.什么是DHCP? DHCP,动态主机配置协议,前身是BOOTP协议,是一个局域网的网络协议,使用UDP协议工作,常用的2个端口:67(DHCP server), ...
- h5 input file ajax实现文件上传
<input type="file" accept="image/*" height="0" class="file_inp ...
- Volley框架的流程图分析
接着上一篇Volley框架的使用,这一篇主要主要讲Volley框架运作的原理.主要使用流程图来叙述,简单的分析了整个流程的过程,具体的请参考源代码或者查看我上一篇在文章末尾添上的链接. 一 ...
- Memcached使用入门
转:http://www.linuxidc.com/Linux/2011-12/49516.htm http://blog.csdn.net/wxwzy738/article/details/2370 ...
- hdu5823(反演dp)
听说3^n也能水过去.. 其实应该是个经典题,求图染色这个np问题. 把问题拆成独立集来进行dp可以在3^n之内水过去. 拆成独立集的时候就发现,等价与一个经典的反演dp问题 然后复杂度就变成了 n* ...
- jquery的tree table(树表)
因项目需要,需要在表格中加入tree,使用了jquery的tree table,经美化,完美兼容各种框架的table: 请移步下载tree table 的js文件及css文件等,http://ludo ...