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 ...
随机推荐
- WPF让人哭笑不得的资源
前几天遇到了一个让我哭笑不得的bug,我写的Wpf程序在Win7里可以运行,到XP.WindowsServer里运行点击某个控件之后闪退,不报任何错,在后台代码里trycatch也捕捉不到任何异常.很 ...
- 个人分享:平时开发中感觉几款不错 IDE 、插件、工具
本人主业 C# 开发,由于是做 Web 开发,所以像 SQL.JavaScript 这些肯定经常要接触到.当然,平时本人也写过 Node.js.Java.Python之类,不过,这些只能讲简单了解而已 ...
- 【转】工科男IT职场求生法则
转自:http://www.36dsj.com/archives/3459 我在IT职场打滚超过10年了,从小小的程序员做到常务副总.相对于其它行业,IT职场应该算比较光明的了,但也陷阱重重,本文说说 ...
- 【转】larbin主要代码说明
转自:http://blog.csdn.net/s030702614/article/details/5683928 1. 主函数: int main (int argc, char *argv[]) ...
- HDU 5590 ZYB's Biology 水题
ZYB's Biology Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid ...
- Apache的Order Allow Deny心得
Allow和Deny可以用于apache的conf文件或者.htaccess文件中(配合Directory, Location, Files等),用来控制目录和文件的访问授权. 所以,最常用的是: O ...
- CF B. Kolya and Tandem Repeat
Kolya got string s for his birthday, the string consists of small English letters. He immediately ad ...
- Android-----使用Button特效 selector+shape
当然除了使用drawable这样的图片外今天谈下自定义图形shape的方法,对于Button控件Android上支持以下几种属性shape.gradient.stroke.corners等. 我们就以 ...
- EasyUI基础入门之Pagination(分页)
前言 对于一些企业级的应用来说(非站点),页面上最为基本的内容也就是表格和form了.对于类似于ERP这类系统来说数据记录比較大,前端表格展示的时候必需得实现分页功能了.恰巧EasyUI就提供了分页组 ...
- purge
RR级别 mysql V5.6 debug ; 测试1 会话1: mysql)); Query OK, rows affected (0.04 sec) mysql,"a"); Q ...