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. 就是普通的斜率优化因为有两 ...
随机推荐
- win7+64位+Oracle+11g+64位下使用P…
1)安装Oracle 11g 64位 2)安装32位的Oracle客户端( instantclient-basic-win32-11.2.0.1.0) 下载instantclient-basi ...
- php封装pdo操作数据的工具类
<?php header("Content-Type:text/html;charset=utf-8"); class PdoMysql{ public static $co ...
- [Makefile] Makefile 及其工作原理
转自:https://www.linuxidc.com/Linux/2018-09/154071.htm 当你需要在一些源文件改变后运行或更新一个任务时,通常会用到 make 工具.make 工具需要 ...
- 交互原型设计软件axure rp学习之路(三)
(三)Axure rp元件的触发事件 l OnClick(点击时): 鼠标点击事件,除了动态面板的所有的其他元件的点击时触发.比如点击按钮. l OnMouseEnter(鼠标移入时): 鼠标进入 ...
- EIP权限工作流平台-移动端
- 2010辽宁省赛G(佩尔方程)
#include <iostream> #include <stdio.h> #include <string.h> #include <algorithm& ...
- 使用ffmpeg编码时,如何设置恒定码率,并控制好关键帧I帧间隔
1. 大家在使用ffmpeg进行视频编码时,使用-b命令,想控制比特率,却发现结果并没有如我们设置所愿,通过码流分析器观察视频码流,码率的波动还是很大的,ffmpeg控制的并不好,这时候,我们可以通过 ...
- Solr 6.7学习笔记(06)-- spell check
拼写检查也是搜索引擎必备的功能.Solr中提供了SpellCheckComponent 来实现此功能.我看过<Solr In Action>,是基于Solr4.X版本的,那时Suggest ...
- duliu——思维+线段树
题目 [题目描述]小 `D` 喜欢出毒瘤题毒人.当然,他的毒瘤更多体现在若干个难题组合在同一场比赛时.小 `D` 脑中有 $n$ 个毒瘤题 idea,第 $i$ 个的毒值为$d_i$.当第 $i$ 个 ...
- falsk-sqlalchemy 连接数据库出现 No module named 'MySQLdb'
安装pymysql:pip install pymysql 按照 Flask-SQLAlchemy 文档的说明,配置好 SQLALCHEMY_DATABASE_URI = 'mysql://usern ...