Cod sursa(job #891805)

Utilizator sebii_cSebastian Claici sebii_c Data 25 februarie 2013 20:14:53
Problema Cuburi2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.04 kb
#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()
{
    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;
}