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. 就是普通的斜率优化因为有两 ...
随机推荐
- Eclipse 安装插件
Eclipse 安装插件 本文介绍Eclipse插件的安装方法.Eclipse插件的安装方法大体有三种:直接复制.使用link文件,以及使用eclipse自带的图形界面的插件安装方法. AD: 做为当 ...
- 快速发现并解决maven依赖传递冲突
此文已由作者翟曜授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 最近在测试过程中,遇到了几次maven传递依赖冲突的问题,所以记录下解决的过程,遇到类似问题供参照. 问题现象 ...
- SCUT - 205 - 饲养牛 - 最大流
https://scut.online/p/205 连着做所以一开始就觉得是网络流. 这种至多分配几次的很有网络流的特征. 一开始想从食物和饮料流向牛,但是怎么搞都不对. 其实可以从s流向食物,食物流 ...
- 715. Range Module
A Range Module is a module that tracks ranges of numbers. Your task is to design and implement the f ...
- js 实现ajax(get和post)
get和post的区别:1.GET产生一个TCP数据包:POST产生两个TCP数据包. 对于GET方式的请求,浏览器会把http header和data一并发送出去,服务器响应200(返回数据): 而 ...
- Solr 6.7学习笔记(06)-- spell check
拼写检查也是搜索引擎必备的功能.Solr中提供了SpellCheckComponent 来实现此功能.我看过<Solr In Action>,是基于Solr4.X版本的,那时Suggest ...
- 【实验吧】该题不简单——writeup
题目地址:http://ctf5.shiyanbar.com/crack/3/ 一定要注意读题: 要求找出用户名为hello的注册码,这八成就是 要写注册机啊! ——————————————————— ...
- acwing 3 完全背包
习题地址 https://www.acwing.com/problem/content/description/3/ 题目描述有 N 种物品和一个容量是 V 的背包,每种物品都有无限件可用. 第 i ...
- dot watch
dot watch+vs code提升asp.net core开发效率 在园子中,已经又前辈介绍过dotnet watch的用法,但是是基于asp.net core 1.0的较老版本来讲解的,在asp ...
- NET Core Hosting
ASP.NET Core 运行原理解剖[1]:Hosting ASP.NET Core 是新一代的 ASP.NET,第一次出现时代号为 ASP.NET vNext,后来命名为ASP.NET 5,随 ...