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

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

#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. 安卓--界面--改变image view

    switch (v.getId()) { case R.id.button: imageView.setImageResource(R.drawable.jelly_bean); break; def ...

  2. python调用NLPIR - ICTCLAS2013实现中文分词

    环境:win7.VS2008.Python2.7.3 第一步:照着文档[2]将NLPIR库封装成Python的扩展: 第二步:新建一个名为“nlpir_demo”的目录,将第一步最后得到的名为“nlp ...

  3. dis进行反编译

    摘录自官方文档: https://docs.python.org/2/library/dis.html 回头自己也脑补下. 可以使用dis查看自己代码的复杂度之类的东西. 比如while 1 和whi ...

  4. yii 初步安装

    第一步: window下点击>开始 >运行CMD命令. 第二步:进入Yiic文件的目录   (例如在D盘里面 D:/yii/framework) 第三步:D:\yii\framework& ...

  5. Cantor的数表

    题目描述 如下数列,前5项分别是1/1,1/2,2/1,3/1,2/2…….输入n,输出第n项. 1/1   1/2   1/3   1/4   1/5 2/1   2/2   2/3   2/4 3 ...

  6. WebForm aspx页面传值---7种方式

    1.get方式 发送页 <form id="form1" runat="server">    <div>        <a h ...

  7. UE4 VR 模式全屏 4.13

    以前写了一个4.11版本全屏,高版本的没用所以也不清楚情况,最近出了4.13,刚好新项目要用上打包出来以后发现,控制台命令fullscreen没有用了, 被stereo on 替代,但是还是没有全屏, ...

  8. UE4 Windows平台部署游戏到IOS 第二部分

    点击加号后会出来如下截图 勾选上红色单选框处(因为这个我已经申请过了所以是灰色),然后continue到后面会出现下图 选择一个之前我提到申请证书会用的的那个.csr后缀文件夹,完成以后就可以下载证书 ...

  9. iOS纯代码适配masonry中mas_的问题

    //equalto 和 mas_equalto 是有区别的.但是我们不打算去了解,可以通过添加以下代码来统一. //注意!! 宏定义必须要放在 import 引入头文件之前! //define thi ...

  10. null、undefined、false、0相等性比较

    之前在看<JavaScript权威指南>的时候看到三个相等性比较的式子: null == undefined ;// ==>true undefined == false;// == ...