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 的容器中,相邻木棒之间不允许重叠,且两边上的木棒,可 ...
随机推荐
- [Effective JavaScript笔记]第1条:了解使用的js版本
1997年 正式成为国际标准,官方名称为ECMAScript. 1999年 定稿第3版ECMAScript标准(简称ES3),最广泛的js版本. 2009年 发布第5版即ES5,引入了一些新特性,标准 ...
- ruby : Exception Notification
https://github.com/smartinez87/exception_notification#sections Add the following line to your applic ...
- sqlmap如何修改线程
找到settings.py文件,具体在\lib\core\目录下找到 # Maximum number of threads (avoiding connection issues and/or Do ...
- python4delphi Python could not be properly initialized. We must quit.
要用32位的DLL,不要用64位的dll Unable to load Python 2.7 dll with Delphi 2010 #6 Closed GoogleCodeExporter op ...
- 2015安徽省赛 B.求和
题目描述 对于正整数n,k,我们定义这样一个函数f,它满足如下规律 现在给出n和k,你的任务就是要计算f(n,k)的值. 输入 首先是一个整数T,表示有T组数据 接下来每组数据是n和k(1<=n ...
- 南洋理工 OJ 115 城市平乱 dijstra算法
城市平乱 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 南将军统领着N个部队,这N个部队分别驻扎在N个不同的城市. 他在用这N个部队维护着M个城市的治安,这M个城市 ...
- Decompiled .class file,bytecode version:51.0(Java 7) Source for 'Android API 23 Platform' not found
今天在Android Studio中访问Java源码的时候,代码上方出现如下提示: 而且方法体中显示介样内容: throw new RuntimeException("Stub!" ...
- Shortest Word Distance
Given a list of words and two words word1 and word2, return the shortest distance between these two ...
- Django之表单字段的选填与后台界面的管理
参考: http://www.crazyant.net/1005.html http://gmingzhe.blog.51cto.com/810664/163051 所有的字段,默认blank=Fal ...
- codeforces B. Color the Fence 解题报告
题目链接:http://codeforces.com/problemset/problem/349/B 题目意思:给定v升的颜料和9个需要花费ad 升的颜料,花费ad 升的颜料意味着得到第d个数字,现 ...