当前位置:网站首页>Maximum average value of continuous interval
Maximum average value of continuous interval
2022-07-26 04:03:00 【Su Pimo】
catalog
Given an array A, And length Len;
Choose a length >= Len The range of [l, r], Its average value is the largest .
a n s = ∑ i = l r A [ i ] r − l + 1 ans = \frac{\sum_{i = l}^{r} {A[i]}}{r - l + 1} ans=r−l+1∑i=lrA[i]
namely , The denominator The smaller it is , molecular The bigger it is . The better .
such as : A = [100, 1, 100], Len = 2[100, 1] and [1, 100] All are 50.5;[100, 1, 100] It's the biggest
First, look at a wrong idea .
Sweep code r Endpoint , Calculate with r For the right endpoint , Maximum .
such as , We have got r-1 For the right endpoint , The largest interval is : [l, r-1]; Assuming the current r The maximum interval of the endpoint is : [ll, r] ( Of course, the length of these two intervals is >= Len Of )
that , Can they deduce ?
First , ll >= l It is verifiable ; therefore , The left endpoint is monotonous ;
Algorithm : ( Calculation [l, r] Value and [l-1, r] Value , Compare ) ( If [l-1, r] Bigger , Then remove it. l-1) ( otherwise , [l, r] That's the answer. )
although , With r The maximum interval of the right endpoint , Its left endpoint is monotonous , namely : [l1, 1][l2, 2][l3, 3], …
There will be : l1 <= l2 <= l3
however , For a fixed right endpoint , Suppose the answer is [l, r], The answer to the previous interval is :[ll, r-1]
that , [ll, r][ll + 1, r][ll + 2, r] … Is it monotonous ??
Not really : [4, 3, 2, 1, 9]Len = 3]
The maximum interval of each right endpoint is : [4, 3, 2][4, 3, 2, 1][2, 1, 9]
about 9 Come on , [4, 3, 2, 1, 9] = 3.8, [3, 2, 1, 9] = 3.75, [2, 1, 9] = 4, It does not conform to monotony
The above algorithm , It seems to lead us into a very confused idea …
Mainly still , From the above equation , Both on the left Variable , It's all unknown ; therefore , under these circumstances , Rashly use greed , Is a bad choice .
From the above equation , If we scan unknown r, be r It is known. .
however : ( Left side ans It is unknown. ) ( On the right side of the molecular , The denominator All carry l, It's also unknown ), That is to say 2 A variable .
We are now , One must be eliminated ; l There are many options for endpoints , So choose to eliminate ans This variable .
边栏推荐
- 中国数据库 OceanBase 入选 Forrester Translytical 数据平台报告
- Zkevm: summary of zkevm and L1 by Mina's CEO
- Can't the container run? The Internet doesn't have to carry the blame
- Trust sums two numbers
- 涂鸦幻彩产品开发包如何使用
- 基于SSM选课信息管理系统
- How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
- KBPC1510-ASEMI大芯片15A整流桥KBPC1510
- JS Base64 encoding and decoding
- laravel8 实现接口鉴权封装使用JWT
猜你喜欢

Six years of automated testing from scratch, I don't regret turning development to testing

cpu和gpu已过时,npu和apu的时代开始

Sentinel fusing and current limiting
![[unity3d shader] character projection and reflection](/img/00/d0d994d88475ea590dc5cb60a6ad65.png)
[unity3d shader] character projection and reflection

Helloworld案例分析

基于SSM选课信息管理系统

Find my technology | the Internet of things asset tracking market has reached US $6.6 billion, and find my helps the market develop

Matlab paper illustration drawing template issue 39 - stairs

A large factory developed and tested one, and strangled its neck with a mouse line

微信小程序实现音乐播放器(4)(使用pubsubjs实现页面间通信)
随机推荐
中国数据库 OceanBase 入选 Forrester Translytical 数据平台报告
How to build an enterprise level OLAP data engine for massive data and high real-time requirements?
JS Base64 encoding and decoding
Dracoo master
Pits encountered by sdl2 OpenGL
PHP object conversion array
php中可以使用取绝对值函数 abs() 将负数转成正数
【Unity3d Shader】角色投影与倒影
Go plus security: an indispensable security ecological infrastructure for build Web3
Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
Supervit for deep learning
Matlab paper illustration drawing template issue 39 - stairs
【云原生】谈谈老牌消息中间件ActiveMQ的理解
PHP method to find the location of session storage file
Booking.com binke Shanghai noodles
PHP implements the algorithm of adding from 1 to 100
2.9.4 Ext JS的布尔对象类型处理及便捷方法
Introduction to UFS CLK gate
括号嵌套问题(建议收藏)
Leetcode: 102. Sequence traversal of binary tree