Pagini recente » Cod sursa (job #2495574) | Cod sursa (job #1185113) | Cod sursa (job #391774) | Cod sursa (job #2795897) | Cod sursa (job #891949)
Cod sursa(job #891949)
#include <cstdio>
#include <algorithm>
using namespace std;
const int MAXN = 250001;
long long height[MAXN];
long long cost(int t, int x, int y)
{
long long 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;
long long clo = cost(lower_half, x, y);
long long chi = cost(upper_half, x, y);
if (clo < chi)
hi = upper_half - 1;
else
lo = lower_half + 1;
}
long long clo = cost(lo, x, y);
long long chi = cost(hi, x, y);
if (clo < chi)
printf("%d %lld\n", lo, clo);
else
printf("%d %lld\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("%lld", &height[i]);
for (int i = 0; i < k; ++i) {
int x, y;
scanf("%d %d", &x, &y);
doit(x, y);
}
return 0;
}