Cod sursa(job #891942)

Utilizator sebii_cSebastian Claici sebii_c Data 25 februarie 2013 21:19:08
Problema Cuburi2 Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.07 kb
#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;
}