DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)
题意:长度为L的金条,将n根金棍尽可能放上去,要求重心在L上,使得价值最大,最多有两条可以长度折半的放上去。
分析:首先长度可能为奇数,先*2。然后除了两条特殊的金棍就是01背包,所以dp[now][j][k]表示当前状态,长度为j,使用了k条特殊金棍获得的最大价值,需要对内存和时间优化。
/************************************************
* Author :Running_Time
* Created Time :2015/10/21 星期三 11:55:40
* File Name :D.cpp
************************************************/
#include <bits/stdc++.h>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e3 + 10;
const int L = 4e3 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-8;
ll dp[2][L][3];
struct P {
int a;
ll v;
}p[N]; ll Max(ll a, ll b) {
return a > b ? a : b;
} int main(void) {
int T, cas = 0; scanf ("%d", &T);
while (T--) {
int n, l; scanf ("%d%d", &n, &l);
l *= 2;
ll ans = 0;
for (int i=1; i<=n; ++i) {
scanf ("%d%lld", &p[i].a, &p[i].v);
p[i].a *= 2;
ans = max (ans, p[i].v);
}
memset (dp, 0, sizeof (dp));
int now = 1;
for (int i=1; i<=n; ++i) {
now = 1 - now;
for (int j=0; j<=l; ++j) {
for (int k=0; k<3; ++k) {
dp[now][j][k] = dp[1-now][j][k];
}
}
for (int j=l; j>=p[i].a/2; --j) {
for (int k=0; k<3; ++k) {
if (j >= p[i].a) dp[now][j][k] = max (dp[now][j][k], dp[1-now][j-p[i].a][k] + p[i].v);
if (k) dp[now][j][k] = max (dp[now][j][k], dp[1-now][j-p[i].a/2][k-1] + p[i].v);
}
}
for (int i=0; i<2; ++i) {
for (int j=0; j<=l; ++j) {
for (int k=0; k<3; ++k) ans = max (ans, dp[i][j][k]);
}
}
}
printf ("Case #%d: %lld\n", ++cas, ans);
} return 0;
}
DP(01背包) UESTC 1218 Pick The Sticks (15CCPC C)的更多相关文章
- ACM学习历程—UESTC 1218 Pick The Sticks(动态规划)(2015CCPC D)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 题目大意就是求n根木棒能不能放进一个容器里,乍一看像01背包,但是容器的两端可以溢出容器,只要两端的木 ...
- UESTC 1218 Pick The Sticks
Time Limit: 15000/10000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit Status ...
- USACO Money Systems Dp 01背包
一道经典的Dp..01背包 定义dp[i] 为需要构造的数字为i 的所有方法数 一开始的时候是这么想的 for(i = 1; i <= N; ++i){ for(j = 1; j <= V ...
- HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解)
HDOJ(HDU).3466 Dividing coins ( DP 01背包 无后效性的理解) 题意分析 要先排序,在做01背包,否则不满足无后效性,为什么呢? 等我理解了再补上. 代码总览 #in ...
- POJ.3624 Charm Bracelet(DP 01背包)
POJ.3624 Charm Bracelet(DP 01背包) 题意分析 裸01背包 代码总览 #include <iostream> #include <cstdio> # ...
- HDOJ(HDU).2546 饭卡(DP 01背包)
HDOJ(HDU).2546 饭卡(DP 01背包) 题意分析 首先要对钱数小于5的时候特别处理,直接输出0.若钱数大于5,所有菜按价格排序,背包容量为钱数-5,对除去价格最贵的所有菜做01背包.因为 ...
- HDOJ(HDU).2602 Bone Collector (DP 01背包)
HDOJ(HDU).2602 Bone Collector (DP 01背包) 题意分析 01背包的裸题 #include <iostream> #include <cstdio&g ...
- UVA.10130 SuperSale (DP 01背包)
UVA.10130 SuperSale (DP 01背包) 题意分析 现在有一家人去超市购物.每个人都有所能携带的重量上限.超市中的每个商品有其相应的价值和重量,并且有规定,每人每种商品最多购买一个. ...
- uestc oj 1218 Pick The Sticks (01背包变形)
题目链接:http://acm.uestc.edu.cn/#/problem/show/1218 给出n根木棒的长度和价值,最多可以装在一个长 l 的容器中,相邻木棒之间不允许重叠,且两边上的木棒,可 ...
随机推荐
- firefox查看微信公众平台的数据分析时就出现不信任链接怎么办?
昨天用360清理垃圾后火狐主页的快速拨号栏消失了,整了半天还是无法使用就重装了一下firefox,导入备份的书签,添加自己所需的附加组件,设置为隐私模式,开始继续体验.按惯例打开微信公众平台,查看数据 ...
- cf.295.B Two Buttons (bfs)
Two Buttons time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...
- Integer Inquiry
Integer Inquiry Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Sub ...
- 理解Java中的接口
一.为什么要使用接口 假如有一个需求:要求实现防盗门的功能.门有"开"和"关"的功能,锁有"上锁"和"开锁"的功能. 分 ...
- xxxx is not translated in zh-rCN, zh-rTW
1.异常提示: "image_content" is not translated in zh-rCN, zh-rTW 2.错误原因: 根据报错提示,是说我没有对string文件做 ...
- [NOIP2015]推销员
[NOIP2015]推销员 试题描述 阿明是一名推销员,他奉命到螺丝街推销他们公司的产品.螺丝街是一条死胡同,出口与入口是同一个,街道的一侧是围墙,另一侧是住户.螺丝街一共有 N 家住户,第 i 家住 ...
- 基础知识《六》---Java集合类: Set、List、Map、Queue使用场景梳理
本文转载自LittleHann 相关学习资料 http://files.cnblogs.com/LittleHann/java%E9%9B%86%E5%90%88%E6%8E%92%E5%BA%8F% ...
- swift 中String,Int 等类型使用注意,整理中
swfit中的String和Int是 struct定义的,不同于NSString和NSNumber, 如果想在一个数组中同时包含String和Int,那么这个数组要声明为[Any] 而不是 [AnyO ...
- python chm 中文帮助 (2.7 和 3.4)
sphinx-build 生成的(htmlhelp) 存在2个问题 1.生成的html 编码 cp2152,需要修改/sphinx/builders/html.py ctx['encoding'] = ...
- std::map常用方法
map<string, int> Employees; Employees["Mike C."] = 12306; Employees.insert(make_pair ...