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 ...
随机推荐
- spring mvc学习(一)入门实例
springMVC处理流程如下: 通过配置DispacherServlet拦截指定的url,让后经HanddlerMapping来决定调用我自定义的Controller,在Controller中经过业 ...
- Fom同时控制每一行不同的状态
代码:app_item_property.set_property('HEADER.MATTER_CODE', alterable,property_off); 实现效果: ...
- Extjs datefield 日历控件中文显示
原版的日历控件选择的时候是英文的,不是中文的.后来将在extjs包中src下locale下ext-lang-zh_CN.js引用进来就汉化了
- 计算Excel中的Sheet个数
$strpath="d:\ee.xlsx"$excel=new-object -comobject excel.application$WorkBook = $excel.Work ...
- HDU 4287 Intelligent IME hash
Intelligent IME Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?p ...
- codeforces Round #258(div2) D解题报告
D. Count Good Substrings time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- SIP入门(二):建立SIPserver
在我的上一篇文章中已经介绍怎样通过SIP软电话直接通话,可是假设须要支持很多其它用户互相通话,同一时候基于安全考虑,须要对用户帐户登录进行验证控制,这些情况下就须要建立SIPserver. SIPse ...
- 【JavsScript】Ember.js
现在,我们经常都可以看到复杂的JavaScript应用程序,由于这些应用程序变得越来越复杂,一长串的jQuery回调语句或者通过应用程序在各个状态执行不同的函数调用,这些做法都会变得无法再让人接受,这 ...
- [Angular2 Router] Load Data Based on Angular 2 Route Params
You can load resource based on the url using the a combination of ActivatedRouteand Angular 2’s Http ...
- MPI编程简单介绍
第三章MPI编程 3.1 MPI简单介绍 多线程是一种便捷的模型,当中每一个线程都能够訪问其他线程的存储空间.因此,这样的模型仅仅能在共享存储系统之间移植.一般来讲,并行机不一定在各处理器之间共享存储 ...