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 ...
随机推荐
- So easy Webservice 3.使用HttpClient工具调用Webservice接口
首先,看看webservice服务调用演示: a) 登录http://www.webxml.com.cn b) 单击手机查询服务 c) 选择要调用的方法 例如: getMobileCodeInfo 输 ...
- How to crack interviews ...
Code practice: Leetcode: www.leetcode.com HackerRank: www.hackerrank.com Topcoder: https://www.topco ...
- bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...
- nyoj 1058部分和问题(DFS)
部分和问题 时间限制:1000 ms | 内存限制:65535 KB 难度:2 描述 给定整数a1.a2........an,判断是否可以从中选出若干数,使它们的和恰好为K. 输入 首先, ...
- Linux下C程序的编译,运行,及调试
先查看linux有没有gcc 和 gdb $ gcc -v $ gdb -v 如果没有安装gcc,可以 $ yum install gcc 要获取管理员权限才能安装软件,$ su root (有的li ...
- iOS - UIToolbar
前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UIToolbar : UIView <UIBarPositioning& ...
- sscanf的用法(转)
队长做上海邀请赛的I题时遇到一个棘手的问题,字符串的处理很麻烦,按传统的gets全部读入的话还要做N多处理,太浪费时间. 回来之后搜了一下sscanf的用法发现可以很好的解决这一类问题,各种百度,转来 ...
- Python学习(2)基本语法
目录 交互式编程 脚本式编程 Python 标识符 Python保留字符 行和缩进 多行语句 Python 引号 Python注释 Python空行 python的输入和输出 命令行参数 交互式编程 ...
- 使用otl,报错:mysql Commands out of sync; you can't run this command now
1.代码如下: void TestCache(otl_connect& otlConn) { try { ] = {}; sprintf(sql,"call test1(1)&quo ...
- 转: 浅谈C/C++中的指针和数组(二)
转自:http://www.cnblogs.com/dolphin0520/archive/2011/11/09/2242419.html 浅谈C/C++中的指针和数组(二) 前面已经讨论了指针和数组 ...