题目链接: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. 如何直接在github网站上更新你fork的repo?

    玩过github的人一定会在你自己的账号上fork了一些github开源项目.这些开源项目往往更新比较活跃,你今天fork用到你自己的项目中去了,过几个星期这个fork的origin可能有一些bugf ...

  2. HTTP全部报文首部字段

    总结了一下HTTP各种报文首部字段. HTTP报文类型与结构 HTTP报文结构 报文首部 空行(CR+LF) 报文主体 HTTP报文类型 http有两种类型报文,请求报文和响应报文两种报文的首部结构如 ...

  3. js方式进行地理位置的定位api搜集

    新浪 //int.dpool.sina.com.cn/iplookup/iplookup.php?format=js //int.dpool.sina.com.cn/iplookup/iplookup ...

  4. UVa 10054 (打印欧拉回路) The Necklace

    将每个颜色看成一个顶点,对于每个珠子在两个颜色之间连一条无向边,然后求欧拉回路. #include <cstdio> #include <cstring> + ; int G[ ...

  5. [转] POJ数学问题

    转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...

  6. PHP学习笔记05——面向对象

    <?php //1. 类的声明(包括构造方法.析构方法) class PersonA { var $name; //成员属性,用var声明 public $age; //当有其他修饰的时候,就不 ...

  7. python练习程序(批量重命名)

    # -*- coding: cp936 -*- import sys,os,string d=0; path="F://test" srcfile=os.listdir(path) ...

  8. 【阅读】提问的智慧+有效的报告BUG

    这两项可谓是我们测试人员的基本要求了,不过实话说的是,我自己这两方面很薄弱.哎!!! 这两篇文章都看过了,不过我在这也记个笔记吧.督促自己!!! <提问的智慧> <如何有效地报告Bu ...

  9. 【Django】Python虚拟环境工具virtualenv

    教程 第一步:安装virtualenv $pip install virtualenv 第二步:开启虚拟环境的python $cd ENV/Scripts $activate.bat #启用virtu ...

  10. ORACLE学习笔记 索引和约束

    /*** 约束 ***/ * 如果某个约束只作用于单独的字段,即可以在字段级定义约束,也可以在表级定义约 束,但如果某个约束作用于多个字段,  必须在表级定义约束* 在定义约束时可以通过CONSTRA ...