Cod sursa(job #770248)

Utilizator vendettaSalajan Razvan vendetta Data 22 iulie 2012 15:23:22
Problema Cuburi2 Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <iostream>
#include <fstream>

using namespace std;

#define nmax 250005
#define inf (1LL<<61)

ifstream f("cuburi2.in");
ofstream g("cuburi2.out");

int n, m, high[nmax];
typedef struct{
    long long cost;
    long long cnt;
}camp;
camp st[nmax], dr[nmax];

void citeste(){

    f >> n >> m;
    for(int i=1; i<=n; i++) f >> high[i];

}

void rezolva(){

    //st[i].cost = costul de a muta toate cuburile din partea stanga a cubului i pe cubul i
    //st[i].cnt = numarul de cuburi pana la cubul i(inclusiv)
    //dr[i].cost/dr[i].cnt la fel...;

    st[1].cost = 0;
    st[1].cnt = high[1];
    for(int i=2; i<=n; i++){
        st[i].cost = st[i-1].cost + st[i-1].cnt;
        st[i].cnt = st[i-1].cnt + 1LL*high[i];
    }

    dr[n].cost = 0;
    dr[n].cnt = high[n];
    for(int i=n-1; i>=1; i--){
        dr[i].cost = dr[i+1].cost + dr[i+1].cnt;
        dr[i].cnt = dr[i+1].cnt + 1LL*high[i];
    }

    //pentru un interval [x,y] ma intereseaza care element are MIN(st[i].cost - st[x].cost + dr[i].cost - dr[y].cost), cu i = x,y;
    for(int i=1; i<=m; i++){
        int x,y;
        f >> x >> y;
        long long rez = inf;
        int poz = 0;
        for(int i=x; i<=y; i++){
            long long _st = st[i].cost - st[x].cost - (1LL*(i-x)*st[x-1].cnt);
            long long _dr = dr[i].cost - dr[y].cost - (1LL*(y-i)*dr[y+1].cnt);
            if (rez > _st+_dr){
                rez = _st + _dr;
                poz = i;
            }
        }
        g << poz << " " << rez << "\n";
    }


}

int main(){

    citeste();
    rezolva();

    f.close();
    g.close();

    return 0;

}