bzoj1904: Musical Water-fence
找出最高的木块,假设在这块木块上无限加水,就会形成一些水池,然后才向两侧溢出
用并查集维护当前在某个位置使水向左/右流动,水会流向哪个水池或从某一侧溢出浪费,当某个水池满时更新并查集
#include<cstdio>
int n,m,mx=,n2;
double ws[],hs[],sum[],al=,ar=;
int f[],ss[],sp=;
int get(int x){
int a=x,c;
while(x!=f[x])x=f[x];
while(x!=f[a])c=f[a],f[a]=x,a=c;
return x;
}
void cal(int w){
if(w<n2)f[w]=w+n2;
else f[w]=w-n2;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;++i){
scanf("%lf%lf",ws+i,hs+i);
if(hs[i]>hs[mx])mx=i;
}
ss[sp=]=mx;
for(int i=mx-;i;--i){
while(hs[ss[sp]]<hs[i])--sp;
ss[++sp]=i;
}
ss[++sp]=;
n2=n+;
for(int i=sp;i;--i){
for(int j=ss[i];j<ss[i-];++j)f[j+]=f[j+n2]=ss[i]+n2,sum[ss[i]]+=ws[j]*(hs[ss[i]]-hs[j]);
}
ss[sp=]=mx;
for(int i=mx+;i<=n;++i){
while(hs[ss[sp]]<hs[i])--sp;
ss[++sp]=i;
}
ss[++sp]=n+;
for(int i=sp;i;--i){
for(int j=ss[i];j>ss[i-];--j)f[j-+n2]=f[j]=ss[i],sum[ss[i]]+=ws[j]*(hs[ss[i]]-hs[j]);
}
for(int i=;i<=m;++i){
int x;double s,sl,sr;
scanf("%d%lf",&x,&s);
sl=sr=s/;
while(sl){
int w=get(x);
if(w%n2==){
al+=sl;
sl=;
}else if(w%n2==n+){
ar+=sl;
sl=;
}else if(sum[w%n2]>sl){
sum[w%n2]-=sl;
sl=;
}else{
sl-=sum[w%n2];
sum[w%n2]=;
cal(w);
}
}
while(sr){
int w=get(x+n2);
if(w%n2==){
al+=sr;
sr=;
}else if(w%n2==n+){
ar+=sr;
sr=;
}else if(sum[w%n2]>sr){
sum[w%n2]-=sr;
sr=;
}else{
sr-=sum[w%n2];
sum[w%n2]=;
cal(w);
}
}
}
printf("%.3f\n%.3f",al,ar);
return ;
}
bzoj1904: Musical Water-fence的更多相关文章
- [luogu P2205] [USACO13JAN]画栅栏Painting the Fence
[luogu P2205] [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to p ...
- 洛谷——P2205 [USACO13JAN]画栅栏Painting the Fence
题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of t ...
- POJ1743 Musical Theme [后缀数组]
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 27539 Accepted: 9290 De ...
- [LeetCode] Pacific Atlantic Water Flow 太平洋大西洋水流
Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...
- [LeetCode] Trapping Rain Water II 收集雨水之二
Given an m x n matrix of positive integers representing the height of each unit cell in a 2D elevati ...
- [LeetCode] Water and Jug Problem 水罐问题
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- [LeetCode] Paint Fence 粉刷篱笆
There is a fence with n posts, each post can be painted with one of the k colors. You have to paint ...
- [LeetCode] Trapping Rain Water 收集雨水
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- [LeetCode] Container With Most Water 装最多水的容器
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
随机推荐
- 多线程随笔知识点总结-NSThread4.1
线程的状态 状态说明 a.新建 实例化线程对象 b.就绪 向线程对象发送start消息,线程对象被加入可调度线程池等待CPU调度;detach方法和performSelectorInBackGroun ...
- HTML5日期输入类型(date)
在很多页面和web应用中都有输入日期和时间的地方,最典型的是订飞机票,火车票,酒店,批萨等网站. 在HTML5之前,对于这样的页面需求,最常见的方案是用Javascript日期选择组件.这几乎是无可争 ...
- iOS--基础控件总结一
UIWindow窗口 UIView视图 UIButten按钮 UILabel文本显示 UITextField输入框 UI TextView多行输入框 UISwitch开关 UISegmentedCon ...
- ReportViewer中修改rdlc图表中显示的数据
将rdlc以xml的格式打开,修改里面的数据,实现方式如下: public MemoryStream GenerateRdlc() { XmlDocument xmlDoc = new XmlD ...
- Neo4j批量插入(Batch Insertion)
新建一个maven工程,这里不赘述如何新建maven工程. 添加Neo4j jar到你的工程 有两种方式: 上网站官网下载jar包,根据自己的系统下载不同的压缩包,详细过程不描述,请自行搜索其他博客 ...
- kernel/printk.c
/* * linux/kernel/printk.c * * Copyright (C) 1991, 1992 Linus Torvalds * * Modified to make sys_s ...
- 由java的八个基本数据类型说开去
Java中定义了四类/八种基本数据类型: 布尔型----boolean 字符型----char 整数型----byte,short,int,long 浮点型----float,double 这八种基本 ...
- 关于从jsp 中 引用 js 中的里层function
在需要引用的方法里: 需要引用的方法 function (){ new js中的父方法().子方法(参数) }
- QT常用资料
QTableView介绍 Qt 自定义 滚动条 样式 QTableWidget控件总结 QTableWidget的使用和美工总结 QT__stylesheet__操作 QSS QT皮肤(QSS)编程 ...
- C语言实现最基本的回射服务器与客户端(服务器用TCP协议回射客户发来的消息)
话不多说,直接上干货,下面两个程序都是linux程序. server.c完整代码: #include <stdio.h>#include <string.h>#include ...