#include <iostream>
#include <cstdio>
using namespace std;
const int MAX = 99999999;
const int MIN = -MAX;
int n, L, U;
int segment[33000 * 4];
int sum[33000];
void build(int node, int begin, int end)
{
if (begin == end)
segment[node] = sum[begin];
else
{
int mid = (begin + end) / 2;
build(2 * node, begin, mid);
build(2 * node + 1, mid + 1, end);
if (segment[2 * node] > segment[2 * node + 1])
segment[node] = segment[2 * node];
else
segment[node] = segment[2 * node + 1];
}
}
int query(int node, int begin, int end, int left, int right)
{
if (left > end || right < begin)
return MIN;
if (begin >= left && end <= right)
return segment[node];
int mid = (begin + end) / 2;
int p1 = query(2 * node, begin, mid, left, right);
int p2 = query(2 * node + 1, mid + 1, end, left, right);
if (p1 > p2)
return p1;
else
return p2;
}
int main()
{
while (scanf("%d", &n) && n)
{
scanf("%d%d", &L, &U);
sum[0] = 0;
for (int i = 1; i <= n; i++)
{
scanf("%d", &sum[i]);
sum[i] += sum[i-1];
}
build(1, 0, n);
int minSum = MAX;
for (int i = L; i <= n; i++)
{
//I - L, I - U的最大值
int l = i - U;
int r = i - L;
if (l < 0)
{
l = 0;
}
int value = sum[i] - query(1, 0, n, l, r);
if (value < minSum)
minSum = value;
}
cout << minSum << endl;
}
return 0;
}
对应例题:soj 1800