Description
There is a straight and long road in the country called the "Rice Road". Along this rice path, R blocks of paddy fields, the coordinates of each paddy field are
is an integer between 1 and L (with 1 and L). These paddy fields are given in the order of the coordinates in a non-decreasing sequence, i.e. for 0≤i <
R, paddy field I coordinates x[i] meet 1≤x[0]≤ ... ≤x[r-1]≤l.
Note: There may be more than one paddy field at the same coordinate.
We plan to build a Yonekura to store as much rice as possible. Like paddy fields, Yonekura will be built on the Rice road, and its
The coordinates are also an integer between 1 and L (with 1 and L). This yonekura can be built on any bit that satisfies the above conditions
, including those where one or more rice fields have existed.
In the harvest season, every paddy field produces just one wagon of rice. In order to transport these rice to Yonekura, it is necessary to hire
Use a lorry driver to transport rice. The driver's charge is $1 for each full lorry transporting a unit. In other words,
The cost of transporting rice from a particular paddy field to the Yonekura is numerically equal to the absolute value of the difference between the field coordinates and the Yonekura coordinates.
Unfortunately, this year's budget is limited, we can only spend B yuan freight. Your mission is to help us find a
Build a Yonekura location where you can collect as much rice as possible.
Input
First row three integers R L B
Next, an integer for each line of the R line represents X[i]
Output
An integer maximum number of rice
Sample Input
5 20 6
1
2
10
12
14
Sample Output
3
HINT
1≤r≤100,000
1≤l≤1,000,000,000
0≤b≤2,000,000,000,000,000HINT Source
http://www.lydsy.com/JudgeOnline/problem.php?id=2600
This problem my personal algorithm is a binary + enumeration, the web should have a better algorithm, not much to say here.
The first two points can be obtained by several yonekura of grain.
Then check, enumerate the left endpoint, and judge the feasibility.
1#include <iostream>2#include <cstdio>3 using namespacestd;4 Long Longf[100010],now,k,ans,a[100010],sum,n,l,b,lef,righ,mid;5 BOOLCheckLong Longx)6 {7 intI,l,r,mi;8 for(i=1; i<=n-x+1;++i)9 {TenL=i; r=i+x-1; Mi= (L+R)/2; Onenow=A[mi]; Asum=now* (mi-l)-(f[mi-1]-f[l-1]) + (F[r]-f[mi])-now* (rmi); - //cout<<x<< ' <<i<< ' <<sum<<endl; - if(sum<=b)return true; the } - return false; - } - intMain () + { -Cin>>n>>l>>b; + for(intI=1; i<=n;++i) cin>>A[i]; Af[0]=0; at for(intI=1; i<=n;++i) f[i]=f[i-1]+A[i]; -lef=0; -righ=n+1; -ans=0; - while(lef<=righ) - { inMid= (Lef+righ)/2; - if(check (mid) = =true) to { +Lef=mid+1; - if(Ans<mid) ans=mid; the } * Elserigh=mid-1; $ } Panax Notoginsengcout<<ans<<Endl; - return 0; the}
Optimizations: Prefixes and
2600 Yonekura Large field of view evaluation