bzoj1649 [Usaco2006 Dec]Cow Roller Coaster
Description
The cows are building a roller coaster! They want your help to design as fun a roller coaster as possible, while keeping to the budget. The roller coaster will be built on
a long linear stretch of land of length L (1 <= L <= 1,000). The roller coaster comprises a collection of some of the N (1 <= N <= 10,000) different interchangable components. Each component i has a fixed length Wi (1 <= Wi <= L). Due to varying terrain, each
component i can be only built starting at location Xi (0 <= Xi <= L-Wi). The cows want to string together various roller coaster components starting at 0 and ending at L so that the end of each component (except the last) is the start of the next component.
Each component i has a "fun rating" Fi (1 <= Fi <= 1,000,000) and a cost Ci (1 <= Ci <= 1000). The total fun of the roller coster is the sum of the fun from each component used; the total cost is likewise the sum of the costs of each component used. The cows'
total budget is B (1 <= B <= 1000). Help the cows determine the most fun roller coaster that they can build with their budget.
Input
* Line 1: Three space-separated integers: L, N and B.
* Lines 2..N+1: Line i+1 contains four space-separated integers, respectively: Xi, Wi, Fi, and Ci.
Output
* Line 1: A single integer that is the maximum fun value that a roller-coaster can have while staying within the budget and meeting all the other constraints. If it is not
possible to build a roller-coaster within budget, output -1.
Sample Input
0 2 20 6
2 3 5 6
0 1 2 1
1 1 1 3
1 2 5 4
3 2 10 2
Sample Output
选用第3条,第5条和第6条钢轨
题意是有一段0到L的区间,要求用一些线段覆盖,每一条线段都有价值和代价,求在将区间完全覆盖(不能重叠)和总代价不超过B的条件下能获得的最大价值
首先很容易想到一种背包dp的方法
先把线段排序,不用说
f[i][j][k]表示前i个线段覆盖0到j的区间代价为k的最大价值
这样100e肯定TLE+MLE,不用说
然后不会优化,去orz了黄巨大才会做
把第一维省掉,但是依然TLE
但是我们发现枚举第i段线段,那么它能更新的只有几个状态
学着黄巨大把方程倒一下,令f[i][j]表示费用为i,能覆盖0到j的最大价值
那么枚举费用k,因为不能重叠,只会更新从当前线段左端点开始的方案
这样只要O(NB)就可以了
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
using namespace std;
struct work{
int l,r,f,c;
}a[10010];
int n,m,b;
LL f[1010][1010],ans=-1;
inline bool cmp(const work &a,const work &b){return a.l<b.l||a.l==b.l&&a.r<b.r;}
inline int max(int a,int b)
{return a>b?a:b;}
inline int read()
{
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
int main()
{
memset(f,-1,sizeof(f));
f[0][0]=0;
n=read();m=read();b=read();
for (int i=1;i<=m;i++)
{
a[i].l=read();
a[i].r=a[i].l+read();
a[i].f=read();
a[i].c=read();
}
sort(a+1,a+m+1,cmp);
for (int i=1;i<=m;i++)
for (int j=a[i].c;j<=b;j++)
if (f[j-a[i].c][a[i].l]!=-1)
f[j][a[i].r]=max(f[j][a[i].r],f[j-a[i].c][a[i].l]+a[i].f);
for (int i=0;i<=b;i++)ans=max(ans,f[i][n]);
printf("%lld\n",ans);
}
bzoj1649 [Usaco2006 Dec]Cow Roller Coaster的更多相关文章
- 【动态规划】bzoj1649 [Usaco2006 Dec]Cow Roller Coaster
很像背包. 这种在一个数轴上进行操作的题常常需要对区间排序. f[i][j]表示距离到i时,花费为j时的权值之和. f[x[i]+l[i]][j+c[i]]=max{f[x[i]][j]+w[i]}( ...
- BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )
有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...
- 【BZOJ】1649: [Usaco2006 Dec]Cow Roller Coaster(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1649 又是题解... 设f[i][j]表示费用i长度j得到的最大乐趣 f[i][end[a]]=ma ...
- BZOJ——1649: [Usaco2006 Dec]Cow Roller Coaster
http://www.lydsy.com/JudgeOnline/problem.php?id=1649 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 7 ...
- bzoj 1649: [Usaco2006 Dec]Cow Roller Coaster【dp】
DAG上的dp 因为本身升序就是拓扑序,所以建出图来直接从1到ndp即可,设f[i][j]为到i花费了j #include<iostream> #include<cstdio> ...
- bzoj1649 / P2854 [USACO06DEC]牛的过山车Cow Roller Coaster
P2854 [USACO06DEC]牛的过山车Cow Roller Coaster dp 对铁轨按左端点排个序,蓝后就是普通的二维dp了. 设$d[i][j]$为当前位置$i$,成本为$j$的最小花费 ...
- Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 554 Solved: 346[ ...
- BZOJ 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐( dfs )
直接从每个奶牛所在的farm dfs , 然后算一下.. ----------------------------------------------------------------------- ...
- 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐
1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 432 Solved: 270[ ...
随机推荐
- 一篇非常经典的springMVC注解实现方式详解
今天公司让搭建个springMVC的注解框架,研究了好半天,网络搜罗了半天,好不容易找到篇,拿来分享下: 原文出处:http://itxxz.com/a/kuangjia/2014/0531/4.ht ...
- QT 4.7支持中文(QT4.7)(中文)(makeqpf)
QT 4.7支持中文(QT4.7)(中文)(makeqpf) 摘要: QT4.7.0在移植到开发板上的时候,中文支持是必不可少的,如何让QT支持中文,如何制作QT支持的字体文件,如何使QT UI编辑器 ...
- C++里消除Wunused
编译程序时,有一大堆警告总是不爽的.别人的代码也就忍了,不好去改.自己的可没法忍.看看C++里怎么消除Wunused警告. 先来看下面的程序: #include <iostream> in ...
- Smart ——jiaoyou模板
<!--{foreach $vip_data as $key=>$volist}--> <!--{if $key==0 ||$key==1||$key==5||$key= ...
- Lua 基本语法
学习Unity的ulua热更新插件就必须先学习lua的基本语法. 我们一起来学习Lua吧O(∩_∩)O. 首先搭建Lua运行环境Lua for windows 下载地址: http://www.cr1 ...
- 汉字与utf8相互转化
NSString* strA = [@"%E4%B8%AD%E5%9B%BD"stringByReplacingPercentEscapesUsingEncoding:NSUTF8 ...
- JS 数组扩展函数--求起始项到终止项和
Array.prototype.sum= function(l,r){ l=l==undefined ? 0 : l; r=r==undefined ? this.length - 1 : r; va ...
- Spring 3.x企业应用开发实战(11)----基于@AspectJ配置切面
1.@AspectJ的JDK必须是JDK 5.0+ 基于@AspectJ配置切面 @AspectJ采用注解描述切点.增强,两者只是表达式方式不同,效果相同. @AspectJ语法基础-----切点表达 ...
- 单调队列-Hdu-4122-Alice's mooncake shop
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4122 题目意思: 一家月饼店,有n个订单,从2001年1月1日0时开始24小时营业开m个小时,且每个 ...
- "a newer version of unity web player is required but the auto-update failed"
问题背景描述: 项目采用winform调用unity web player作为播放器在客户端使用. 在有些环境会出现标题所示错误. 经过一翻研究后发现是插件在向服务器请求更新以下文件时报http 30 ...