题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3872

  题意:有n个龙珠按顺序放在一列,每个龙珠有一个type和一个权值,要求你把这n个龙珠分成k个段,每段的权值是这段中的最大的权值,使得最后的权值之和最小。但是现在有个要求,分的段中,龙珠的type不能和最右边的相等。

  容易想到是一个DP:f[i]=Min{f[j]+Min(j,i) | j是满足要求的点}。直接搞的话O(n^2),显然超时了。但是可以发现,这个Min(j,i)是有分段性的,因此我们可以维护一个单调递减的栈,那么只要求栈中的元素就可以了,因为只有这些元素有效。这里还要求最小值,需要维护两课线段树,一颗是维护f[i]最小,还有一棵是f[j]+min(相对应的栈中的元素)。此题细节比较多,容易出错>,<,,,,也可能是我很久没刷题,残了T^T

 //STATUS:C++_AC_1796MS_8848KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End LL low[N<<][],f[N];
int l[N],la[N],q[N][],type[N],eng[N];
int T,n; void update(int l,int r,int rt,int w,LL val,int flag)
{
if(l==r){
low[rt][flag]=val;
return;
}
int mid=(l+r)>>;
if(w<=mid)update(lson,w,val,flag);
else update(rson,w,val,flag);
low[rt][flag]=Min(low[rt<<][flag],low[rt<<|][flag]);
} LL query(int l,int r,int rt,int L,int R,int flag)
{
if(L<=l && r<=R){
return low[rt][flag];
}
int mid=(l+r)>>;
LL ret=LNF;
if(L<=mid)ret=Min(ret,query(lson,L,R,flag));
if(R>mid)ret=Min(ret,query(rson,L,R,flag));
return ret;
} int binary(int l,int r,int tar)
{
int mid;
while(l<r){
mid=(l+r)>>;
if(q[mid][]<tar)l=mid+;
else r=mid;
}
return l;
} int main()
{
// freopen("in.txt","r",stdin);
int i,j,t,L,R,top;
LL lowf;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
mem(la,);
for(i=;i<=n;i++){
scanf("%d",&type[i]);
l[i]=la[type[i]];
la[type[i]]=i;
}
for(i=;i<=n;i++){
scanf("%d",&eng[i]);
} mem(low,INF);mem(f,INF);f[]=;
update(,n+,,,,);
q[top=][]=;q[top][]=INF;
for(i=;i<=n;i++){
while(q[top][]<=eng[i]){
update(,n+,,q[top-][],LNF,);
top--;
}
q[++top][]=i;q[top][]=eng[i];
t=q[top-][];
lowf=query(,n+,,t,i-,);
update(,n+,,t,lowf+eng[i],); L=binary(,top+,l[i]);
f[i]=query(,n+,,q[L][],i,);
if(l[i]<q[L][]){
lowf=query(,n+,,l[i],q[L][]-,);
f[i]=Min(f[i],lowf+q[L][]);
}
update(,n+,,i,f[i],);
} printf("%I64d\n",f[n]);
}
return ;
}

HDU-3872 Dragon Ball 线段树+DP的更多相关文章

  1. HDU 4362 Dragon Ball 线段树

    #include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...

  2. HDU 5125 magic balls(线段树+DP)

    magic balls Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  3. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  4. HDU.1556 Color the ball (线段树 区间更新 单点查询)

    HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...

  5. Tsinsen A1219. 采矿(陈许旻) (树链剖分,线段树 + DP)

    [题目链接] http://www.tsinsen.com/A1219 [题意] 给定一棵树,a[u][i]代表u结点分配i人的收益,可以随时改变a[u],查询(u,v)代表在u子树的所有节点,在u- ...

  6. hdu 5700区间交(线段树)

    区间交 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

  7. Snacks HDU 5692 dfs序列+线段树

    Snacks HDU 5692 dfs序列+线段树 题意 百度科技园内有n个零食机,零食机之间通过n−1条路相互连通.每个零食机都有一个值v,表示为小度熊提供零食的价值. 由于零食被频繁的消耗和补充, ...

  8. HDU 1556 Color the ball(线段树区间更新)

    Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...

  9. HDU 4362 Dragon Ball 贪心DP

    Dragon Ball Problem Description   Sean has got a Treasure map which shows when and where the dragon ...

随机推荐

  1. Toast.makeText().show() 正常使用但不显示的解决办法

    症状: toast正常构造,调用show时,不显示. View tabMeItem = tabHost.findViewById(R.id.tab_me_xc); tabMeItem.setOnCli ...

  2. HTTP 请求方式: GET和POST的比较

    GET和POST是HTTP的两个常用方法.   什么是HTTP? 超文本传输协议(HyperText Transfer Protocol -- HTTP)是一个设计来使客户端和服务器顺利进行通讯的协议 ...

  3. emplace_back减少内存拷贝和移动

    --------<深入应用C++11:代码优化与工程级应用>第2章使用C++11改进程序性能,本章将分别介绍右值引用相关的新特性.本节为大家介绍emplace_back减少内存拷贝和移动. ...

  4. 利用XPath读取Xml文件

    之所以要引入XPath的概念,目的就是为了在匹配XML文档结构树时能够准确地找到某一个节点元素.可以把XPath比作文件管理路径:通过文件管理路 径,可以按照一定的规则查找到所需要的文件:同样,依据X ...

  5. hdu1052(田忌赛马 贪心)

    好坑的一道题,不过确实是贪心的一道好题,想了好久一直无法解决平局的情况.  参考了别人的思路,然后结合了自己的想法,总算是想出来了. 这题有些步骤是必须要执行的,有四个步骤 一.当期状态田忌的最慢的马 ...

  6. laravel, Composer和autoloading

    http://www.php-fig.org/psr/psr-4/ http://www.php-fig.org/psr/psr-0/ http://alanstorm.com/laravel_com ...

  7. 随便谈谈alphago与人机大战

    3月16日历时8天的人机大战终于落下帷幕,alphago以4:1的比分击败了当年如日中天的李世石.这个结果让我这个围棋爱好者+计算机爱好者百感交集…… ——一个时代落幕了,一个新的时代开启了. 这次人 ...

  8. UVa 10859 Placing Lampposts

    这种深层递归的题还是要多多体会,只看一遍是不够的 题意:有一个森林,在若干个节点处放一盏灯,灯能照亮与节点邻接的边.要求:符合要求的放置的灯最少为多少,在灯数最少的前提下,一条边同时被两盏灯照亮的边数 ...

  9. 51nod1201 整数划分

    01背包显然超时.然后就是一道神dp了.dp[i][j]表示j个数组成i的方案数.O(nsqrt(n)) #include<cstdio> #include<cstring> ...

  10. Jave 鼠标点击画太极 PaintTaiji (整理)

    package demo; /** * Jave 鼠标点击画太极 PaintTaiji (整理) * 声明: * 又是一份没有注释的代码,而且时间已经久远了,不过代码很短,解读起来应该 * 不会很麻烦 ...