hdu 4719 动态规划
思路:dp[i]表示到第i个点为结尾能获得的最大值,那么dp[i]=h[i]*h[i]+dp[i-x]-h[i-x];(i-l<=x<=i);那么我们可以转换下,以dp[i]-h[i]为新的权值,动态方程就变成了dp[i]=dp[i-x]+h[i]*h[i](i-l<=x<=i);只要找到最大的dp[i-x]就可以了。可以用线段树维护。
#include<set>
#include<cmath>
#include<queue>
#include<cstdio>
#include<vector>
#include<string>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pb push_back
#define mp make_pair
#define Maxn 100010
#define Maxm 80002
#define LL __int64
#define Abs(x) ((x)>0?(x):(-x))
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define inf 10000000
#define lowbit(x) (x&(-x))
#define clr(x,y) memset(x,y,sizeof(x))
#define Mod 1000000007
using namespace std;
int h[Maxn],sorted[Maxn];
int r[Maxn];
LL dp[Maxn],ans;
struct Tree{
int l,r;
LL Max;
int mid(){
return (l+r)>>;
}
}tree[Maxn*];
int cmp(int a,int b)
{
if(sorted[a]==sorted[b])
return b<a;
return sorted[a]<sorted[b];
}
void BuildTree(int l,int r,int po)
{
tree[po].l=l,tree[po].r=r,tree[po].Max=-;
if(l==r) return ;
int mid=tree[po].mid();
BuildTree(l,mid,lson(po));
BuildTree(mid+,r,rson(po));
}
void update(int i,LL val,int po)
{
if(tree[po].l==tree[po].r){
tree[po].Max=val;
return ;
}
int mid=tree[po].mid();
if(i<=mid)
update(i,val,lson(po));
else
update(i,val,rson(po));
tree[po].Max=max(tree[lson(po)].Max,tree[rson(po)].Max);
}
void query(int i,int po)
{
if(i==)
return ;
if(tree[po].r==i){
ans=max(ans,tree[po].Max);
return ;
}
int mid=tree[po].mid();
if(i<=mid)
query(i,lson(po));
else{
query(mid,lson(po));
query(i,rson(po));
}
}
int main()
{
int t,i,j,Case=,n,l;
scanf("%d",&t);
while(t--){
clr(dp,-);
scanf("%d%d",&n,&l);
for(i=;i<=n;i++){
scanf("%d",h+i);
sorted[i]=h[i];
}
for(i=;i<=n;i++) r[i]=i;
sort(r+,r+n+,cmp);
int pos[Maxn];
for(i=;i<=n;i++) pos[r[i]]=i;
BuildTree(,n,);
for(i=;i<=n;i++){
ans=-;
query(pos[i]-,);
if(i<=l)
dp[i]=(LL)h[i]*h[i];
if(ans>=)
dp[i]=max(dp[i],(LL)h[i]*h[i]+ans);
update(pos[i],dp[i]-(LL)h[i],);
if(i-l>)
update(pos[i-l],-,);
}
printf("Case #%d: ",++Case);
if(dp[n]<)
printf("No solution\n");
else
printf("%I64d\n",dp[n]);
}
return ;
}
hdu 4719 动态规划的更多相关文章
- hdu 1087 动态规划之最长上升子序列
http://acm.hdu.edu.cn/showproblem.php?pid=1087 Online Judge Online Exercise Online Teaching Online C ...
- HDU 1003 动态规划
http://acm.hdu.edu.cn/showproblem.php?pid=1003 这几天开始刷动归题目,先来一道签到题 然而做的并不轻松, 没有注意到边界问题, WA了几发才发现 #inc ...
- hdu 4055 && hdu 4489 动态规划
hdu 4055: 一开始我想的递推方向想得很复杂,看了别人的博客后才醍醐灌顶: 参照他的思路和代码: #include<cstdio> #include<cstring> # ...
- hdu 4745 动态规划
思路:特水的一个最长回文子序列动态规划.比赛时硬卡第一题,49WA后终于AC,可惜没时间做这题,结果成绩也就可想而知了.兔子跳一样权值的石头,并且一个正跳,一个反跳,这不就是个回文子序列吗?????! ...
- hdu 4711 动态规划
思路:其实这题是个挺水的动态规划,一开始就能AC,可是不知道错哪了,瞎改瞎交,WA了数十次.AC之后怎么改都是AC,也不知道改了什么地方,郁闷死了~~~难道开始时的测试数据有问题??? dp[i][j ...
- HDU 6076 (动态规划)
HDU 6076 Security Check Problem : 有两个长度为n的队列过安检,每个人有一个特征值.如果两个队列中的第一个人的特征值之差小于等于k,那么一次只能检查其中一个人,否则一次 ...
- HDU 1171 Big Event in HDU (动态规划、01背包)
Big Event in HDU Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- hdu 3280 动态规划
思路:dp[i][j]表示区间i,j变为回文串最少的代价. #include<map> #include<set> #include<cmath> #include ...
- hdu 4055 动态规划
#include<map> #include<set> #include<cmath> #include<queue> #include<cstd ...
随机推荐
- JAVA核心技术--继承
1.继承:向上追溯,对同一批类的抽象,延续和扩展父类的一切信息! 1)关键字:extends 例如,父类是Animal,子类是Dog; eg: public class Dog exte ...
- JQuery 弹出层,始终显示在屏幕正中间
1.让层始终显示在屏幕正中间: 样式代码: .model{ position: absolute; z-index: 1003; width:320px; height:320px; text-ali ...
- 内存不足(OutOfMemory)的调试分析
32位操作系统的寻址空间是4G,其中有2G被操作系统占用,也就是说留给用户进程的内存只有2G(其中还要扣除程序加载时映像占用的部分空间,一般只有1.6G~1.8G左右可以使用). 如果进程运行中需要申 ...
- oracle存储过程实例
oracle存储过程实例 分类: 数据(仓)库及处理 2010-05-03 17:15 1055人阅读 评论(2)收藏 举报 认识存储过程和函数 存储过程和函数也是一种PL/SQL块,是存入数据库的P ...
- iOS开发——动画OC篇&知识点总结
图层与动画知识点总结 1.Core Animation 非娱乐类的软件都会用到的动画,操作简单. 2.Quartz 2D绘图 是一个2D绘图引擎. (1) 绘图Context是一个绘图的目标对象,定义 ...
- iOS开发——实用OC篇&多种定时器详细介绍
多种定时器详细介绍 在软件开发过程中,我们常常需要在某个时间后执行某个方法,或者是按照某个周期一直执行某个方法.在这个时候,我们就需要用到定时器. 然而,在iOS中有很多方法完成以上的任务,到底有 ...
- boost.asio源码剖析(四) ---- asio中的泛型概念(concepts)
* Protocol(通信协议) Protocol,是asio在网络编程方面最重要的一个concept.在第一章中的levelX类图中可以看到,所有提供网络相关功能的服务和I/O对象都需要Protoc ...
- oc-28-构造函数
Dog.h #import <Foundation/Foundation.h> @interface Dog : NSObject { NSString *_name; int _age; ...
- Lucene的DocFieldProcessor类
DocFieldProcessor类的任务1 按顺序存储所有的field和对应的fieldinfo2 为当前这篇doc的field按照fieldname来建立hash索引3 调用InvertedDoc ...
- php 换行 空格分割处理
<?php function parse_specification($specification){ $rt=array(); $lines=array_filter(preg_split(& ...