Two Buildings

本文介绍了一种算法,用于在一系列建筑物中寻找两个宽度为1,高度分别为hi和hj的建筑物,使得它们的高度之和与差的乘积最大。通过分治策略和决策单调性,程序能够快速找到最优解,适用于高度数据范围达1百万的大型问题。

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;
}

<think>好的,我现在需要帮用户批阅这篇英语题。首先,我得通读一遍文章,了解整体内容和结构。看起来这是一篇关于北京融合古今的短文,用户可能是在做语法或词汇的改错练习。接下来,我需要逐句检查,找出标有数字的错误部分,并给出修改建议。 先看第一个错误:(1) royal palaces。这里前面的形容词narrow修饰的是hutong,而hutong和后面的royal palaces应该是并列结构,所以royal前面可能需要加上连词and,或者考虑其他修饰问题。不过原句已经有and连接hutong和royal palaces,可能问题在于royal的大小写?或者是单复数?仔细看看,royal palaces作为专有名词可能不需要大写,但这里可能正确,所以可能错误在于前面的并列结构是否需要重复and。不过原句已经有and连接hutong和royal palaces,所以可能正确,但用户标了(1),可能这里需要改为复数?或者是冠词问题?可能royal palaces前缺少定冠词the?比如the royal palaces?但原句用的是小写,可能不需要。或者可能应该用形容词形式,比如royal palaces是正确的,所以可能没有错误?这时候可能需要再仔细考虑用户标记的位置,可能在hutong和royal palaces之间是否需要逗号或连接词。原句是“narrow hutong and(1) royal palaces”,可能这里and后面应该用逗号分隔,或者结构有问题。可能应该改为“narrow hutong, and royal palaces”,或者在hutong后加逗号?不过可能用户的问题在于royal palaces是否正确,或者是否需要其他修饰。暂时标记为正确,可能用户有其他错误。 接下来是(2) builds。前面的动词是keep,后面应该是名词,所以builds改为built,作为过去分词修饰system,即carefully built system。这里应该是built,因为是被建造的系统,被动语态。 然后是(3) where。这里where引导的定语从句缺少主语,因为where是关系副词,后面需要接完整的句子结构。而原句是“Beijing is a place where welcomes...”,这里where后面缺少主语,应该改为which,因为place作为先行词,后面接which作为主语,即“a place which welcomes...”。 (4) wonderful。这里architectural是形容词,后面需要名词,而wonderful是形容词,不能直接跟在后面,应该改为名词形式wonders,即“architectural wonders”。 (5) somehow。这里句子结构没问题,但可能需要确认连接词是否正确。原句是“but somehow these...”,正确,所以可能没有错误,但用户标记了(5),可能需要检查是否有其他问题,比如标点或连接词。可能正确,所以暂时标记为正确,但需要再确认。 (6) several times。这里“Visiting several times...”可能语法没问题,但根据上下文,可能需要用Having visited,因为动作发生在过去,对现在有影响,所以用现在分词完成式。即“Having visited several times...”。 (7) I has amazed。这里的主语是I,所以谓语动词应该用have been amazed,即现在完成时的被动语态,正确形式是“I have been amazed”。 (8) to record。spend time doing something,所以这里应该用动名词形式recording,即“spent the last two years recording”。 (9) remarking。这里应该是形容词,表示显著的,正确拼写是remarkable,所以改为remarkable development。 (10) means。主语是development,单数,所以谓语动词用单数形式means,正确。但需要检查主谓一致,这里正确,所以可能没有错误。用户可能误标,或者有其他问题,比如句子结构是否需要调整,但根据上下文,这里正确。 最后,通读一遍修改后的文章,确保所有错误都被纠正,语句通顺,语法正确。同时,注意用户可能的深层需求,比如希望提高英语写作能力,或者准备考试,所以解释错误原因和修改建议能帮助他们理解并避免再犯类似错误。</think>以下是针对这篇英语短文的批阅及修改建议(标注对应原文序号): --- ### **一、语法/用词错误修正** 1. **and(1) royal palaces → and royal palaces** - 正确,无需修改。原文结构无误,但建议在hutong后加逗号: *"...narrow hutong**,** and royal palaces..."* 2. **builds(2) → built** - "carefully builds system" 语法错误,需用过去分词表被动: *"carefully **built** system of ring roads"* 3. **where(3) → which** - "a place where welcomes..." 中缺少主语,需用关系代词 **which**: *"a place **which** welcomes the fast-paced development..."* 4. **wonderful(4) → wonders** - "architectural wonderful" 词性错误,需名词形式: *"21st-century architectural **wonders**"* 5. **but(5) somehow → 正确** - 无需修改,衔接自然。 6. **Visiting (6) → Having visited** - 需用完成时强调过去多次访问的影响: ***"Having visited** several times over the last 10 years..."* 7. **I has amazed(7) → I have been amazed** - 主语为"I",需用现在完成时被动语态: *"I **have been amazed** by the co-existence..."* 8. **to record(8) → recording** - "spend time doing sth" 固定搭配: *"spent the last two years **recording** everything..."* 9. **remarkable(9) → remarkable** - 拼写错误,应为 **remarkable**: *"The **remarkable** development of this city..."* 10. **means(10) → 正确** - 主谓一致无误,无需修改。 --- ### **二、优化建议** 1. **逻辑衔接** - 第二段开头可加过渡词(如 *However*)增强对比: *"**However,** for all its ancient buildings..."* 2. **标点与冠词** - 专有名词前加定冠词: *"the narrow hutong" → "the narrow **hutongs**"*(复数更自然) *"Buddhist temples" → **"the** Buddhist temples"* 3. **句式多样性** - 避免重复使用"Beijing is...",可替换为: *"As a city that bridges the ancient and modern, Beijing..."* --- ### **三、修改后完整版** ```plaintext Beijing is a city bridging the ancient and the modern. From the Buddhist temples to museums, the narrow hutongs, and royal palaces, it is home to more than 3,000 years of glorious history, even down to its layout, with the city keeping its carefully built system of ring roads. However, for all its ancient buildings, Beijing is also a place which welcomes the fast-paced development of modern life, with 21st-century architectural wonders standing side by side with historical buildings of the past. It is a distinct visual contrast that shouldn’t work, but somehow these two very different worlds make a good combination. Having visited several times over the last 10 years, I have been amazed by the coexistence of old and new, and how a city was able to keep such a rich heritage while constantly growing. As a photographer, I have spent the last two years recording everything I discovered. The remarkable development of this city, which is consciously designed to protect the past while stepping into the modern world, means there is always something new to discover here, and I could be photographing Beijing for the next 50 years. ``` --- ### **四、重点错误总结** | 错误类型 | 例句修正 | 规则说明 | |---------|---------|---------| | 主谓一致 | I **have been** amazed | 主语"I"需搭配have | | 非谓语动词 | spent...**recording** | "spend + time + doing" | | 关系代词 | place **which** welcomes | which指代place作主语 | | 词性混淆 | architectural **wonders** | 形容词+名词搭配 | 建议加强练习:**非谓语动词用法** 和 **主谓一致规则**。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值