Two Buildings

【分治】【决策单调性】
There are n buildings along a horizontal street. The buildings are next to each other along the street, and the i-th building from left to right has width 1 and height hi. Among the n buildings, we are to find two buildings, say the i-th building and j-th building with i<j, such that (hi+hj)∗(j−i) is maximized.

For example, the right figure shows 5 buildings, with heights 1, 3, 2, 5, 4, from left to right. If we choose the first 2 buildings, then we get (1+3)∗(2−1)=4. If we choose the first and fifth buildings, then we (1+4)∗(5−1)=20. The maximum value is achieved by the second and fifth buildings with heights 3 and 4, respectively: (3+4)∗(5−2)=21.

Write a program that, given a sequence of building heights, prints max1≤i<j≤n(hi+hj)∗(j−i).

Input
Your program is to read from standard input. The input starts with a line containing an integer n (2≤n≤1,000,000), where n is the number of buildings. The buildings are numbered 1 to n from left to right. The second line contains the heights of n buildings separated by a space such that the i-th number is the height hi of the i-th building (1≤hi≤1,000,000).

Output
Your program is to write to standard output. Print exactly one line. The line should contain max1≤i<j≤n(hi+hj)∗(j−i).

Examples

Input
5
1 3 2 5 4
Output
21

Input
5
8 3 6 3 1
Output
36

解析:对于任意i,j 若存在k>j并且h[k]>h[j]则j不可能是我们想要的右界,可以把他排除,同理也可以对左界进行排除,然后左界的数据和右界的数据进行配对找出最大值

对于(hi+hj)∗(j−i)等于(hi-(-hj))∗(j−i)可以表示成一个矩形的面积

而对于左右界的数据来说:先去中间值mid再右侧找到使结果最大的pos点,对于mid左侧的数据来说(右侧同理)

图片转自https://www.cnblogs.com/wjyyy/p/CF102920L.html

所以只需要找到mid和pos配对 以及mid左侧的点和pos及左侧进行配对,以及mid右侧的点和pos及右侧的点进行配对

AC代码:

#include<iostream>
using namespace std;
int a[1000010];
int zz[1000010][2];
int yy[1000010][2];
int conz=0,cony=0;
long long ans=0;
long long jc(int x,int y)
{return 1LL*(zz[x][0]+yy[y][0])*(yy[y][1]-zz[x][1]);
}
void fact(int l1,int r1,int l2,int r2)
{int mid=l1+r1>>1;int pos=l2;for(int i=l2;i<=r2;i++)if(jc(mid,i)>jc(mid,pos))pos=i;ans=max(ans,jc(mid,pos));if(mid-1>=l1) fact(l1,mid-1,pos,r2);if(mid+1<=r1) fact(mid+1,r1,l2,pos);
}
int main()
{int n;cin>>n;for(int i=1;i<=n;i++) scanf("%d",a+i);int pos1=0,pos2=0;for(int i=1;i<=n;i++){if(a[i]>pos1) pos1=a[i],zz[++conz][0]=a[i],zz[conz][1]=i;if(a[n-i+1]>pos2) pos2=a[n-i+1],yy[++cony][0]=a[n-i+1],yy[cony][1]=n-i+1;}fact(1,conz,1,cony);cout<<ans<<endl;
}

Two Buildings相关推荐

  1. 2015 Multi-University Training Contest 2 1002 Buildings

    Buildings Problem's Link:  http://acm.hdu.edu.cn/showproblem.php?pid=5301 Mean: n*m列的网格,删除一个格子x,y,用矩 ...

  2. Shortest Distance from All Buildings

    Shortest Distance from All Buildings 题目链接:https://leetcode.com/problems... 这道题要求最短的距离,一般这种要求可以到的地方的距 ...

  3. LTE Module User Documentation(翻译5)——Mobility Model with Buildings

    LTE用户文档 (如有不当的地方,欢迎指正!) 8 Mobility Model with Buildings 我们现在通过例子解释如何在 ns-3 仿真程序中使用 buildings 模型(特别是 ...

  4. HDU4372 Count the Buildings

    HDU4372 Count the Buildings 题目描述 Solution 思路很妙. 考虑从最高的一个楼房将序列分开,左边可以看到FFF个,右边可以看到BBB个,一个楼房可以被看到,则一定是 ...

  5. LeetCode Shortest Distance from All Buildings

    原题链接在这里:https://leetcode.com/problems/shortest-distance-from-all-buildings/ 题目: You want to build a ...

  6. 【Paper】Short Term Electric Energy Consumption Prediction in in Smart Buildings Methods Survey

    论文原文:点击此处 论文年份:2019 论文被引:10(2020/08/06/) 43(2022/03/26) 文章目录 A Comparative Study of Time Series Fore ...

  7. Learn from Architects of Buildings

     Learn from Architects of Buildings Keith Braithwaite Architecture is a social act and the material ...

  8. 2020-2021 ACM-ICPC, Asia Seoul Regional Contest L. Two Buildings (决策单调性 分治)

    linkkkkkk 题意: 给定长度为nnn的数组ccc,求max((j−i)∗(ci+cj))max((j-i)*(c_i+c_j))max((j−i)∗(ci​+cj​)) 思路: 将式子转化为m ...

  9. 《Energy and Buildings》期刊介绍(SCI 2区)

    期刊官方网站 期刊投稿网址 期刊投稿小助手 介绍 Energy and Buildings is an international journal publishing articles with e ...

  10. Google Earth Engine(GEE)——全球建筑物矢量图免费下载Open Buildings V1 Polygons

    之前我们已经介绍过了关于微软全球建筑物提取的矢量图. 全球建筑物提取数据集(免费下载):微软/GlobalMLBuildingFootprints_此星光明2021年博客之星云计算Top3的博客-CS ...

最新文章

  1. 【Luogu】P3927 SAC E#1 - 一道中档题 Factorial
  2. java web 监控cpu、内存等。hyperic-sigar
  3. 红管2不显示服务器,红色管弦乐队2怎么建立服务器 | 手游网游页游攻略大全
  4. C++ Linux下线程池的实现
  5. 【GIT】使用Git命令窗口将本地工程提交至远程GitHub
  6. Nginx 基础 ( 二)
  7. lgg6可以root的版本_Kali Linux 2020.1版本变更内容
  8. 完美世界2020编程题-救雅典娜 英雄AB PK
  9. PL/SQL Developer Initialization erro
  10. oracle mysql认证考试流程_报考oraclemysql认证考试流程有些什么步骤
  11. 学校做计算机教室锐捷,锐捷“云课堂”:先改变桌面云,再改变教室
  12. 蓝桥杯——摔手机 动态规划
  13. 翻译 Duckietown an Open, Inexpensive and Flexible Platform for Autonomy Education and Research
  14. 阿里巴巴Java成神之路-笔记(8)动态代理
  15. 15、Spark_RDD算子——AggregateByKey
  16. BZOJ 4892: [Tjoi2017]DNA(SA+RMQ / SAM)
  17. 三大运营商最新的手机号段分配情况
  18. 徕卡 sl android app,随时随地无缝互联 徕卡发布最新版FOTOS App
  19. 年薪50万的程序员_毕业之后,这些年薪50万+的90后程序员经历了什么?-阿里云开发者社区...
  20. OpenMVG、OpenMVS配置及学习记录(Win10+VS2015)

热门文章

  1. dxf制作kml_如何导入矢量边界文件,kml  shap.dxf 格式
  2. RepVGG:让VGG风格的ConvNet再次伟大起来
  3. python识别图片内容并获取两个地点间的距离
  4. 深度解读达芬奇架构:华为AI芯片的“秘密武器”
  5. Sublime Text常用快捷键及插件配置
  6. 数字IC验证:总线握手协议(VALID/READY握手机制)
  7. 记事本-java课程设计_java课程设计----记事本
  8. 【原创理论】 数据处理•数据沉淀法则
  9. php无极限分类讲解,php无限极分类入门教程
  10. Linux i2c总线(2) - I2C核心层