找出最高的木块,假设在这块木块上无限加水,就会形成一些水池,然后才向两侧溢出

用并查集维护当前在某个位置使水向左/右流动,水会流向哪个水池或从某一侧溢出浪费,当某个水池满时更新并查集

#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的更多相关文章

  1. [luogu P2205] [USACO13JAN]画栅栏Painting the Fence

    [luogu P2205] [USACO13JAN]画栅栏Painting the Fence 题目描述 Farmer John has devised a brilliant method to p ...

  2. 洛谷——P2205 [USACO13JAN]画栅栏Painting the Fence

    题目描述 Farmer John has devised a brilliant method to paint the long fence next to his barn (think of t ...

  3. POJ1743 Musical Theme [后缀数组]

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 27539   Accepted: 9290 De ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. [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 ...

  8. [LeetCode] Trapping Rain Water 收集雨水

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  9. [LeetCode] Container With Most Water 装最多水的容器

    Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...

随机推荐

  1. JavaScript获取一段html片段中a标签的href值

    最近,做项目中有一个需求,页面中有一个文本编辑器,里面写的内容最后生成了html代码片段,在另一个页面需要前一个页面文本编辑器的html代码片段中的a标签的href值,就尝试做了,因为不太熟悉js,所 ...

  2. navicat在ubuntu下中文乱码的真正解决方法ZT

    乱码解决方法(自己解决我自己这种情况之后打猜测): 打开start_navicat文件,会看到 export LANG="en_US.UTF-8" 将这句话改为 export LA ...

  3. sql语句查询结果添加排序列(转)

      给查询出的SQL记录添加序号列,解决方法有以下两种 第一: select ROW_NUMBER() OVER (ORDER BY a.字段 ASC) AS XUHAO,a.* from table ...

  4. 12、c#中事务及回滚

    public void UpdateContactTableByDataSet(DataSet ds, string strTblName) { try { SqlDataAdapter myAdap ...

  5. Guid与id区别

    一.产生Guid的方法 1.SqlServer中使用系统自带的NEWID函数: select NEWID() 2.C#中,使用Guid类型的NewGuid方法: Guid gid;           ...

  6. js - 奇怪的回调探索

    做一个手机端页面是发现的奇怪的问题,函数调用的问题(回调). 一句话描述: 某一个dom元素绑定的事件函数在全局能trigger方法调用,但是在ajax成功回调函数里不能被trigger方法调用. 具 ...

  7. 随机生成UUID(GUID)的方法

    - (NSString *)UUID { CFUUIDRef uuid_ref = CFUUIDCreate(NULL); CFStringRef uuid_string_ref= CFUUIDCre ...

  8. NSCharacter​Set在字符串操作中得使用

    NSCharacterSet以及它的可变版本NSMutableCharacterSet,用面向对象的方式表示一组Unicode字符,它经常与NSString及NSScanner组合起来使用,在不同的字 ...

  9. Android 性能测试

    写在前面: 测试一道,博主接触的也是皮毛而已,没有接触过rom的测试,下边所说的都是博主接触过的app的性能测试.我只谈方法,少提概念.各位大神不喜勿喷. 概述 除启动时间外,我们应该做的测试,可能需 ...

  10. IplImage结构体

    一.IplImage的一些重要成员: 1.origin:图像原点的定义.=0,则图片的左上角是原点:=1,则左下角是原点.                                  IplIm ...