UOJ #205/BZOJ 4585 【APIO2016】Fireworks 可并堆+凸包优化Dp
4585: [Apio2016]烟火表演
Time Limit: 40 Sec Memory Limit: 256 MB
Submit: 115 Solved: 79
[Submit][Status][Discuss]
Description


Input
所有的输入均为正整数。令 N代表分叉点的数量, M代表烟花的数量。分叉点从1 到N 编
Output
输出调整导火索长度,让所有烟花同时爆炸,所需要的最小代价
Sample Input
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
Sample Output
#include<cstdio> typedef long long ll;
const int len(),N();
int n,m,fa[len+]; ll sum,C[len+];
int max(int a,int b){return a>b?a:b;}
template<class T>void read(T &x)
{
x=;bool f=;char c=getchar();
while((c<''||c>'')&&c!='-')c=getchar();if(c=='-')f=,c=getchar();
while(c>=''&&c<=''){x=x*+c-'';c=getchar();}
x=f?-x:x;
}
struct Leftist{ll val;int dis,nx[];}Lheap[N<<];int tot,root[N],son[N];
void swap(int &x,int &y){if(x==y)return;x^=y;y^=x;x^=y;}
int merge(int x,int y)
{
if(!x||!y)return x|y;
if(Lheap[x].val<Lheap[y].val)swap(x,y);
Lheap[x].nx[]=merge(Lheap[x].nx[],y);
if(Lheap[Lheap[x].nx[]].dis<Lheap[Lheap[x].nx[]].dis)swap(Lheap[x].nx[],Lheap[x].nx[]);
Lheap[x].dis=Lheap[Lheap[x].nx[]].dis+;
return x;
}
//int slope(int x){return son[x]-Lheap[root[x]].size+1;}//返回x所在堆顶元素的斜率,好像错了
void pop(int x)
{
if(!x)return;
root[x]=merge(Lheap[root[x]].nx[],Lheap[root[x]].nx[]);
}
void deal(int x,int y,int w)//处理x的儿子y,其边长w
{
while(--son[y])pop(y);
//等价于:while(slope(y)>0&&Lheap[root[y]].size)pop(y);
//有son[y]个斜率大于等于0,最后一个等于0
ll R=Lheap[root[y]].val; pop(y);
ll L=Lheap[root[y]].val; pop(y);
Lheap[++tot]=(Leftist){L+w,,,};
root[y]=merge(root[y],tot);
Lheap[++tot]=(Leftist){R+w,,,};
root[y]=merge(root[y],tot);
root[x]=merge(root[x],root[y]);
}
int main()
{
// freopen("C.in","r",stdin);
read(n),read(m);
for(int i=;i<=n+m;i++) read(fa[i]),read(C[i]),sum+=C[i];
// fprintf(stderr,"1\n");
for(int i=n+m;i>=;i--)
{
if(i>n)
{
Lheap[++tot]=(Leftist){C[i],,,};
root[fa[i]]=merge(root[fa[i]],tot);
Lheap[++tot]=(Leftist){C[i],,,};
root[fa[i]]=merge(root[fa[i]],tot);//L=R
}else deal(fa[i],i,C[i]);
son[fa[i]]++;
// fprintf(stderr,"%d \n",i);
}
while(son[]--)pop();//等于0没贡献
int tp=;
for(tp=;root[];tp++)
C[tp]=Lheap[root[]].val,pop();
for(int i=tp-;i>=;i--)//不好算初始斜率,但知道k(i+1)=k(i)+1,(C[i]-C[i+1])*k(i)+(C[i-1]-C[i])*k(i+1)....
//C(tp)不算,C(tp)即最左边的拐点是按道理是0,但是因为上面是没有平移,只有相对间距,所以就忽略C(tp)。
sum-=C[i];
printf("%lld\n",sum);
return ;
}
UOJ #205/BZOJ 4585 【APIO2016】Fireworks 可并堆+凸包优化Dp的更多相关文章
- bzoj 4585: [Apio2016]烟火表演【左偏树】
参考:https://blog.csdn.net/wxh010910/article/details/55806735 以下课件,可并堆部分写的左偏树 #include<iostream> ...
- UOJ#7. 【NOI2014】购票 | 线段树 凸包优化DP
题目链接 UOJ #7 题解 首先这一定是DP!可以写出: \[f[i] = \min_{ancestor\ j} \{f[j] + (d[j] - d[i]) * p[i] + q[i]\}\] 其 ...
- CF372C Watching Fireworks is Fun(单调队列优化DP)
A festival will be held in a town's main street. There are n sections in the main street. The sectio ...
- BZOJ 1010 玩具装箱toy(四边形不等式优化DP)(HNOI 2008)
Description P教授要去看奥运,但是他舍不下他的玩具,于是他决定把所有的玩具运到北京.他使用自己的压缩器进行压缩,其可以将任意物品变成一堆,再放到一种特殊的一维容器中.P教授有编号为1... ...
- bzoj 1911 [Apio2010]特别行动队(斜率优化+DP)
1911: [Apio2010]特别行动队 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 3191 Solved: 1450[Submit][Statu ...
- BZOJ 1492: [NOI2007]货币兑换Cash [CDQ分治 斜率优化DP]
传送门 题意:不想写... 扔链接就跑 好吧我回来了 首先发现每次兑换一定是全部兑换,因为你兑换说明有利可图,是为了后面的某一天两种卷的汇率差别明显而兑换 那么一定拿全利啊,一定比多天的组合好 $f[ ...
- bzoj 2131 : 免费的馅饼 (树状数组优化dp)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=2131 思路: 题目给出了每个馅饼的下落时间t,和位置p,以及价值v,我们可以得到如下状态 ...
- BZOJ 1010: [HNOI2008]玩具装箱toy(斜率优化dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1010 题意: 思路: 容易得到朴素的递归方程:$dp(i)=min(dp(i),dp(k)+(i-k ...
- BZOJ 3963 HDU3842 [WF2011]MachineWorks cdq分治 斜率优化 dp
http://acm.hdu.edu.cn/showproblem.php?pid=3842 写的check函数里写的<但是应该是<=,调了一下午,我是个zz. 就是普通的斜率优化因为有两 ...
随机推荐
- 基于Go实现的秒杀系统
这是基于Go语言的一个秒杀系统,这个系统分三层,接入层.逻辑层.管理层.项目源码:https://github.com/BlueSimle/SecKill 系统架构图 秒杀接入层 从Etcd中加载秒杀 ...
- Centos7 使用 supervisor 管理进程
一.安装 //直接使用pip安装(pip的安装 http://www.cnblogs.com/yxhblogs/p/8971251.html) pip install supervisor 二.配置 ...
- 封装类似thinkphp连贯操作数据库的Db类(简单版)。
<?php header("Content-Type:text/html;charset=utf-8"); /** *php操作mysql的工具类 */ class Db{ ...
- L2-013. 红色警报 (并查集)
战争中保持各个城市间的连通性非常重要.本题要求你编写一个报警程序,当失去一个城市导致国家被分裂为多个无法连通的区域时,就发出红色警报.注意:若该国本来就不完全连通,是分裂的k个区域,而失去一个城市并不 ...
- opengl1
OpenGL Programming Guide Programming Guide > Chapter 1 Chapter 1 Introduction to OpenGL Chapter O ...
- HDU-6395 多校7 Sequence(除法分块+矩阵快速幂)
Sequence Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- python 学习笔记8 (模块)
Python 模块(Module),是一个 Python 文件,以 .py 结尾,包含了 Python 对象定义和Python语句. 模块让你能够有逻辑地组织你的 Python 代码段. 把相关的代码 ...
- junit 直接执行测试方法报错
在eclipse中,直接右击test1->Run As->Junit Test,报下面的错: java.lang.NoSuchMethodError: org.junit.runner.R ...
- PostgreSQL 务实应用(五/5)常用表达
在实际应用中,对于具体的数据计算我们会找相应的函数来实现.而计算需求不同的表达,往往会使得我们使用不同的函数或方式来实现.或者也可以说,同一计算可以使用多种不同的表达方式实现. PostgreSQL ...
- Oracle数据库恢复之resetlogs
实验环境:RHEL 5.4 + Oracle 11.2.0.3 如果是一名合格的Oracle DBA,对resetlogs这种关键字都应该是极其敏感的,当确认需要这种操作时一定要三思而后行,如果自己不 ...