乡间有一条笔直而长的路称为“米道”。沿着这条米道上 R 块稻田,每块稻田的坐标均
为一个 1 到 L 之间(含 1 和 L)的整数。这些稻田按照坐标以不减的顺序给出,即对于 0 ≤ i <
R,稻田 i 的坐标 X[i]满足 1 ≤ X[0] ≤ ... ≤ X[R-1] ≤ L。
注意:可能有多块稻田位于同一个坐标上。
我们计划建造一个米仓用于储存尽可能多的稻米。和稻田一样,米仓将建在米道上,其
坐标也是一个 1 到 L 之间的整数(含 1 和 L)。这个米仓可以建在满足上述条件的任一个位
置上,包括那些原来已有一个或多个稻田存在的位置。
在收获季节,每一块稻田刚好出产一滿货车的稻米。为了将这些稻米运到米仓,需要雇
用一位货车司机来运米。司机的收费是每一满货车运送一个单位的距离收取 1 元。換言之,
将稻米从特定的稻田运到米仓的费用在数值上等于稻田坐标与米仓坐标之差的绝对值。
不幸的是,今年预算有限,我们至多只能花费 B 元运费。你的任务是要帮我们找出一个
建造米仓的位置,可以收集到尽可能多的稻米。
第一行 三个整数 R L B
接下来R行 每行一个整数 表示X[i]
1 tot -= abs(a[pos-(r&1)]-a[pos-(r&1)-mid]);
2 tot += abs(a[pos+mid]-a[pos]);
1 //It is made by Awson on 2017.10.17
2 #include <set>
3 #include <map>
4 #include <cmath>
5 #include <ctime>
6 #include <stack>
7 #include <queue>
8 #include <vector>
9 #include <string>
10 #include <cstdio>
11 #include <cstdlib>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 #define LL long long
16 #define Min(a, b) ((a) < (b) ? (a) : (b))
17 #define Max(a, b) ((a) > (b) ? (a) : (b))
18 #define Abs(x) ((x) < 0 ? (-(x)) : (x))
19 using namespace std;
20 const int N = 100000;
21
22 LL n, m, b;
23 LL a[N+5];
24
25 bool check(LL r) {
26 LL mid = r>>1, pos = mid+(r&1), tot = 0;
27 for (int i = 1; i <= r; i++) tot += Abs(a[i]-a[pos]);
28 if (tot <= b) return true;
29 for (pos++; pos+mid <= n; pos++) {
30 tot -= Abs(a[pos-(r&1)]-a[pos-(r&1)-mid]);
31 tot += Abs(a[pos+mid]-a[pos]);
32 if (tot <= b) return true;
33 }
34 return false;
35 }
36 void work() {
37 scanf("%lld%lld%lld", &n, &m, &b);
38 for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
39 LL L = 1, R = n, ans = 0;
40 while (L <= R) {
41 LL mid = (L+R)>>1;
42 if (check(mid)) ans = mid, L = mid+1;
43 else R = mid-1;
44 }
45 printf("%lld\n", ans);
46 }
47 int main() {
48 work();
49 return 0;
50 }