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. 就是普通的斜率优化因为有两 ...
随机推荐
- Maximum Cardinality Bipartite Matching: Augmenting Path Algorithm
http://www.csie.ntnu.edu.tw/~u91029/Matching.html int nx,ny; int mx[N],my[N]; bool vy[N]; bool g[N][ ...
- SPFA算法——最短路径
粗略讲讲SPFA算法的原理,SPFA算法是1994年西南交通大学段凡丁提出 是一种求单源最短路的算法 算法中需要用到的主要变量 int n; //表示n个点,从1到n标号 int s,t; //s ...
- 解决 Ajax 发送 post 请求出现 403 Forbidden 的三种方式
众所周知前端向后台发送 post 请求时,必须验证 csrf,否则会报错 403 Forbidden.使用 Django Form 表单可以直接在表单里面添加 {% csrf_token %} 即可, ...
- python-根据URL地址下载文件
博主个人网站:https://chenzhen.online 使用Python中提供的urllib.request下载网上的文件 #coding=utf-8 """ 目标 ...
- [UE4]C++设置AnimInstance的相关问题
注意:ue4 4.17调用LoadObject<UAnimBlueprintGeneratedClass>直接崩 http://aigo.iteye.com/blog/2285001 UA ...
- 洛谷P1556 幸福的路
P1556 幸福的路 题目描述 每天,John都要为了农场里N(1≤N≤10)头牛的健康和幸福四处奔波. 每头牛的位置可以描述为一个二维坐标,John从坐标原点(0,0)出发.为了使路径更有趣,Joh ...
- 优化MyEclipse编译速度慢的问题
转载大神的 https://www.cnblogs.com/luxd/p/5999217.html
- Proto.Actor模型
Proto.Actor模型 http://proto.actor/ https://github.com/axzxs2001/ProtoActorSample https://www.cnblogs. ...
- Ocelot实现API网关服务
NET Core微服务之基于Ocelot实现API网关服务 https://www.cnblogs.com/edisonchou/p/api_gateway_ocelot_foundation_01. ...
- windows/Linux下设置ASP.Net Core开发环境并部署应用
10分钟学会在windows/Linux下设置ASP.Net Core开发环境并部署应用 创建和开发ASP.NET Core应用可以有二种方式:最简单的方式是通过Visual Studio 2017 ...