Pagini recente » Cod sursa (job #2568340) | Cod sursa (job #464711) | Cod sursa (job #240054) | Cod sursa (job #3163034) | Cod sursa (job #891942)
Cod sursa(job #891942)
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 250001;
int height[MAXN];
int cost(int t, int x, int y)
{
int res = 0;
for (int i = x; i <= y; ++i)
res += abs(t - i) * height[i];
return res;
}
void doit(int x, int y)
{
int lo = x, hi = y;
while (hi - lo > 1) {
int lower_half = (2 * lo + hi) / 3;
int upper_half = (lo + 2 * hi) / 3;
int clo = cost(lower_half, x, y);
int chi = cost(upper_half, x, y);
if (clo < chi)
hi = upper_half - 1;
else
lo = lower_half + 1;
}
int clo = cost(lo, x, y);
int chi = cost(hi, x, y);
if (clo < chi)
printf("%d %d\n", lo, clo);
else
printf("%d %d\n", hi, chi);
}
int main()
{
freopen("cuburi2.in", "r", stdin);
freopen("cuburi2.out", "w", stdout);
int n, k;
scanf("%d %d", &n, &k);
for (int i = 1; i <= n; ++i)
scanf("%d", &height[i]);
for (int i = 0; i < k; ++i) {
int x, y;
scanf("%d %d", &x, &y);
doit(x, y);
}
return 0;
}