bzoj 2809 左偏树\平衡树启发式合并
2014-03-29 13:09
197 查看
首先我们对于一颗树,要选取最多的节点使得代价和不超过m,那么我们可以对于每一个节点维护一个平衡树,平衡树维护代价以及代价的和,那么我们可以在logn的时间内求出这个子树最多选取的节点数,然后对于一个节点的平衡树我们可以由他的子节点启发式合并而来,时间复杂度nlog^2n。
这道题还可以用左偏树来解决,左偏树为一种可合并堆,合并,删除,插入都在logn内完成,那么这道题的时间复杂度还可以nlogn。
反思:我写的是左偏树的,手残把value打成cost了= =,查了半天。
这道题还可以用左偏树来解决,左偏树为一种可合并堆,合并,删除,插入都在logn内完成,那么这道题的时间复杂度还可以nlogn。
反思:我写的是左偏树的,手残把value打成cost了= =,查了半天。
/************************************************************** Problem: 2809 User: BLADEVIL Language: C++ Result: Accepted Time:3604 ms Memory:8060 kb ****************************************************************/ //By BLADEVIL #include <cstdio> #include <algorithm> #define maxn 100010 #define LL long long using namespace std; int n,m,l; int left[maxn],right[maxn]; int pre[maxn],other[maxn],last[maxn]; LL ans; LL cost[maxn],value[maxn],size[maxn],sum[maxn]; void connect(int x,int y) { pre[++l]=last[x]; last[x]=l; other[l]=y; } int combine(int x,int y) { if ((!x)||(!y)) return x+y; if (cost[x]<cost[y]) swap(x,y); right[x]=combine(right[x],y); sum[x]=sum[left[x]]+sum[right[x]]+cost[x]; size[x]=size[left[x]]+size[right[x]]+1; return x; } int work(int x) { sum[x]=cost[x]; size[x]=1; int rot=x; for (int p=last[x];p;p=pre[p]) rot=combine(rot,work(other[p])); while (sum[rot]>m) rot=combine(left[rot],right[rot]); ans=max(ans,value[x]*size[rot]); return rot; } int main() { scanf("%d%d",&n,&m); for (int i=1;i<=n;i++) { int x; scanf("%d%lld%lld",&x,&cost[i],&value[i]); if (x) connect(x,i); } work(1); printf("%lld\n",ans); return 0; }
相关文章推荐
- mysqlbinlog 查看mysql bin 日志
- 光照贴图深入学习 Lightmapping In-Depth
- FOJ2014年3月赛部分题解. FZOJ 2152 2154 2155 2156 2159
- RequireJS实战-优化oschina前端JS(完)
- Apache 配置文件详解
- Apache 配置文件详解
- 单链表相交问题(无环 &有环)
- About Lightmap Baking
- javascript之函数
- Booting ARM Linux
- .net GridView中 A标签runat="server"问题
- 查看 Apache并发请求数及其TCP连接状态
- 微软100题-天天做-第21题
- 查看 Apache并发请求数及其TCP连接状态
- POJ:3928 序列
- NYOJ--相同的和
- Thinking in Java 泛型章节中不用LinkedList来实现自己的内部链式存储机制
- [maven + spring]SpringInAction 注册自定义属性编辑器时出现问题
- Lvs+heartbeat高可用高性能web站点的搭建
- 轻量级java web实践-7(框架源码-5)