Cod sursa(job #973460)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 14 iulie 2013 16:08:01
Problema Stalpi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.02 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

#define MAXN 100002

using namespace std;

struct Pole
{
    Pole(int xx, int l, int r, int c) :
        x(xx),
        Left(l),
        Right(r),
        Cost(c)
    {}

    int x = 0;
    int Left = 0;
    int Right = 0;
    int Cost = 0;
};

ostream& operator << (ostream& os, Pole pole)
{
    os << pole.x << " " << pole.Left << " " << pole.Right << " " << pole.Cost;
    return os;
}

bool operator < (const Pole& lhs, const Pole& rhs)
{
    return lhs.x < rhs.x;
}

long long leftCosts[MAXN];
long long rightCosts[MAXN];

int main()
{
    int n;
    vector<Pole> vPoles;
    fstream fin("stalpi.in", fstream::in);
    fstream fout("stalpi.out", fstream::out);

    fin >> n;
    //cout << n << endl;

    vPoles.reserve(n+2);

    vPoles.push_back(Pole(0, 0, 0, 0));
    for (int i=0; i<n; ++i)
    {
        int x, l, r, c;
        fin >> x >> c >> l >> r;
        
        vPoles.push_back(Pole(x, l, r, c));
    }
    
    sort(begin(vPoles), end(vPoles));
    
    int maxPoleX = vPoles.back().x;
    
    vPoles.push_back(Pole(maxPoleX + 1, 0, 0, 0));
    
    ostream_iterator<int> itOut(cout, " ");
    ostream_iterator<Pole> itOutPoles(cout, "\n");
    
    //copy_n(begin(vPoles), n + 2, itOutPoles);
    //cout << endl;
    
    for (int i=1; i<=n; ++i)
    {
        Pole pole(max(1, vPoles[i].x - vPoles[i].Left), 0, 0, 0); 
        auto it = lower_bound(begin(vPoles), begin(vPoles) + i + 1, pole) - 1;
    
        //cout << it->x << " " << it->Left << " " << it->Right << " " << it->Cost << endl;
        
        leftCosts[i] = 1LL<<52;
        for (int index = it - begin(vPoles); index < i; ++index)
        {
            leftCosts[i] = min(leftCosts[i], vPoles[i].Cost + leftCosts[index]);
        }
    }
    //cout << endl;
    
    //copy_n(begin(leftCosts), n + 2, itOut);
    //cout << endl << endl;

    for (int i=n; i>0; --i)
    {
        Pole pole(min(maxPoleX, vPoles[i].x + vPoles[i].Right), 0, 0, 0); 
        auto it = upper_bound(begin(vPoles) + i , end(vPoles), pole);

        //cout << it->x << " " << it->Left << " " << it->Right << " " << it->Cost << endl;

        rightCosts[i] = 1LL<<52;

        for (int index = it - begin(vPoles); index > i; --index)
        {
            rightCosts[i] = min(rightCosts[i], vPoles[i].Cost + rightCosts[index]);
        }
    }
    //cout << endl;
    
    //copy_n(begin(rightCosts), n + 2, itOut);
    //cout << endl << endl;
    /*pole = Pole(10, 0, 0, 0); 
    it = upper_bound(begin(vPoles), end(vPoles), pole);

    cout << it->x << " " << it->Left << " " << it->Right << " " << it->Cost << endl;*/
    
    long long minPole = 1LL << 52;
    for (int i=1; i<=n; ++i)
    {
        minPole = min(minPole, leftCosts[i] + rightCosts[i] - vPoles[i].Cost);
        
        //cout << minPole << " ";
    }
    //cout << endl;
    
    fout << minPole << "\n";

    return 0;
}