当前位置:网站首页>AcWing. 102 best cattle fence
AcWing. 102 best cattle fence
2022-07-26 04:03:00 【Su Pimo】
catalog
Title Description
Farmer John's farm is owned by N Fields make up , There are a certain number of cattle in every field , No less than 1 head , Not more than 2000 head .
John wants to fence a part of the continuous field , And make the average number of cattle contained in each piece of land in the enclosed area reach the maximum .
The enclosed area shall at least contain F Block plot , among F It will give .
Under the given conditions , Calculate the average of the number of cattle contained in each piece of land in the enclosed area, what is the maximum possible value .
Input format
Enter the integer in the first line N and F, Data is separated by spaces .
Next N That's ok , Enter an integer for each line , The first i+1 The integer entered on the line represents the number i The number of cattle included in the slice area .
Output format
Output an integer , Represents the maximum value of the average multiplied by 1000 Again Rounding down And then the results .
Data range
1 ≤ N ≤ 100000 1≤N≤100000 1≤N≤100000
1 ≤ F ≤ N 1≤F≤N 1≤F≤N
sample input :
10 6
6
4
2
10
3
8
5
9
4
1
sample output :
6500
Using double pointers or greed is wrong , It is not monotonous .
such as : [4, 3, 2, 1, 9]F = 3
With 1 The maximum at the end is : [4, 3, 2, 1] = 2.5
With 9 The biggest thing at the end is : [2, 1, 9] = 4
Here is 3 individual , It's not monotonous ![4, 3, 2, 1, 9] = 3.8[3, 2, 1, 9] = 3.7[2, 1, 9] = 4
The key is : S [ r ] − S [ l ] r − l = a n s \frac{S[r] - S[l]}{r - l} = ans r−lS[r]−S[l]=ans, ans Of course it's unknown , Even if you are fixed r after , One more l, It's also unknown !
so , Think of two points , Dichotomy can take one of the variables ans, Become constant .
and , notice Maximum or Minimum , You should go to Two points Think about it !
1, Define a binary domain D by : All possible averages , [1, 2000] The real number
2, Definition Calc( x) by : All legal intervals ( The length of the >=F The range of ) in , Is there an average >= x The range of .
Whether there is an interval ‘ [ l + 1 , r ] ‘ , Its average value , namely ( S [ r ] − S [ l ] r − l ) , yes ≥ x Of namely S [ r ] − S [ l ] r − l > = x , Simplification : ∑ i = l + 1 r A [ i ] ≥ ( r − l ) ∗ x ∑ i = l + 1 r ( A [ i ] − x ) ≥ 0 Whether there is an interval `[l+1, r]`, Its average value , namely (\frac{S[r] - S[l]}{r - l}), yes \ge x Of \\ namely \frac{S[r] - S[l]}{r - l} >= x, Simplification : \sum_{i = l+1}^{r}{A[i]} \ge (r - l) * x\\ \sum_{i = l+1}^{r}{(A[i] - x)} \ge 0 Whether there is an interval ‘[l+1,r]‘, Its average value , namely (r−lS[r]−S[l]), yes ≥x Of namely r−lS[r]−S[l]>=x, Simplification :∑i=l+1rA[i]≥(r−l)∗x∑i=l+1r(A[i]−x)≥0
namely , structure : B[i] = A[i] - x, Find whether there is continuous length >= F The range of , The sum is >=0 Of , This can be done by : The prefix and + Linear scanning achieves .
Calc( x) = true, x <= ansCalc( x) = false, x > ans
Conform to Boolean duality .
Code
bool Check( double _avg){
S[ 0] = double( A[ 0]) - _avg;
FOR_( i, 1, N-1, 1){
S[ i] = S[ i - 1] + double( A[ i]) - _avg;
}
double ma = S[ Len - 1];
double pre_min = 0;
//< pre_min Must be 0, It cannot be an illegal value . such as : [1, 0, 1, 0], Len=3
//< S[3] yes 0, explain : The sum of the first four is 0. namely , pre_min yes 0, Description is the whole prefix
FOR_( i, Len, N-1, 1){
pre_min = min( pre_min, S[ i - Len]);
ma = max( ma, S[ i] - pre_min);
}
return ma >= 0;
}
void __Solve(){
scanf("%d%d", &N, &Len);
FOR_( i, 0, N-1, 1){
scanf("%d", &A[ i]);
}
double l = 1, r = 2000;
constexpr double Eps_ = 1e-8;
while( r - l > Eps_){
double mid = (l + r) / 2;
if( Check( mid)){
l = mid;
}
else{
r = mid;
}
}
printf("%d", int( r * 1000.0)); //< Must be r, the reason being that ( Round down )
边栏推荐
- Worked overtime for a week to develop a reporting system. This low code free it reporting artifact is very easy to use
- STM32 state machine programming example - full automatic washing machine (Part 2)
- Supervit for deep learning
- 智装时代已来,智哪儿邀您一同羊城论剑,8月4日,光亚展恭候
- 【云原生之kubernetes】kubernetes集群下ConfigMap使用方法
- Wechat applet to realize music player (4) (use pubsubjs to realize inter page communication)
- General test case writing specification
- Dat of deep learning
- Wechat applet realizes music player (5)
- Sentinel fusing and current limiting
猜你喜欢

Wechat applet to realize music player (4) (use pubsubjs to realize inter page communication)

day03_ 1_ Idea tutorial

ZK snark: about private key, ring signature, zkksp

Data elements

MySQL索引失效场景以及解决方案

深度学习之SuperViT

Verilog implementation of key dithering elimination

加班一周开发了报表系统,这个低代码免费IT报表神器太好用了

Bracket nesting problem (recommended Collection)

Luoda Development -- the context of sidetone configuration
随机推荐
leetcode: 102. 二叉树的层序遍历
MySQL index failure scenarios and Solutions
ASEMI整流桥GBU1510参数,GBU1510规格,GBU1510封装
Trust sums two numbers
php eval() 函数可以将一个字符串当做 php 代码来运行
微信小程序实现音乐播放器(5)
WAF details
Web测试方法大全
2.9.4 Ext JS的布尔对象类型处理及便捷方法
Failed to install the hcmon driver
[MCU simulation project] external interrupt 0 and 1 control two digit nixie tube to count
Basic line chart: the most intuitive presentation of data trends and changes
【数字IC/FPGA】热独码检测
redux
Implementation of distributed lock
Go Plus Security:一款Build Web3不可或缺的安全生态基础设施
5 years, 1.4W times, NFT og's road to immortality Web3 column
A large factory developed and tested one, and strangled its neck with a mouse line
1311_ Hardware design_ Summary of ICT concept, application, advantages and disadvantages
Leetcode: 102. Sequence traversal of binary tree