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 ...
随机推荐
- input标签写CSS时需要注意的几点(先收藏)
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-05-05) 飞鱼的声纳顶部的搜索框让我头疼了很长时间,原因是总不能获得跨浏览器的统一样式.主要的问题有这么两个:一是inpu ...
- 对于C语言中数组名是指针的理解
我们都知道,c语言中数组名是一个指针,比如下面这段代码 #include<iostream>using namespace std;int main(){ int a[4]={1,2,3, ...
- JSP页面上用横线代替文本框
<tr> <td class="content" colspan = 4> <input type="hidden" name=& ...
- 图片原理解说(综合版:JPEG,PNG,BMP,GIF)
JPEG: 首先说明 jpeg图片:JPEG是joint Photographic Experts Group(联合图像专家组)的缩写,文件后辍名为".jpg"或".jp ...
- Myeclipse如何改变编码方式
Windows---->Preferences---->myeclipse Enterprise Workbench---->File and Editors----->JSP ...
- 【M27】要求或者禁止对象产生于heap之中
1.要求对象只能产生于heap之中,该怎么办? 栈上的对象肯定调用构造方法和析构方法(离开作用域的时候),因此,要求对象只能产生于heap之中,也就是禁止栈上产生对象,解决办法有两种:将所有的构造方法 ...
- AJAX在Post中文的时候乱码的解决方法
加上设置字符编码的方法: response.setHeader("charset","gb2312"); *************************** ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
- Visual Studio原生开发的10个调试技巧(一)
最近碰巧读了Ivan Shcherbakov写的一篇文章,<11个强大的Visual Studio调试小技巧>.这篇文章只介绍了一些有关Visual Studio的基本调试技巧,但是还有其 ...
- 【JavaScript】 2013年度最强AngularJS资源合集
http://www.iteye.com/news/28651-AngularJS-Google-resource AngularJS是Google开源的一款JavaScript MVC框架,弥补了H ...