Cod sursa(job #973593)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 14 iulie 2013 19:53:57
Problema Stalpi Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 3.14 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;
}

pair<int, int> vRightSortedPoles[MAXN];
long long costs[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));
    
    for (int i=0; i<n; ++i)
    {
        vRightSortedPoles[i] = make_pair(vPoles[i+1].x + vPoles[i+1].Right, i+1);
    }
    
    sort(begin(vRightSortedPoles), begin(vRightSortedPoles) + n);

    int maxPoleX = vRightSortedPoles[n-1].first + 1;
    
    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=0; i<n; ++i)
    {
        cout << vRightSortedPoles[i].first << " " << vRightSortedPoles[i].second << endl;
    }
    cout << endl;*/
    
    for (int i=1; i<=n; ++i)
    {
        costs[i] = 1LL << 61;
    }
    
    for (int i=0; i<n; ++i)
    {
        long long minVal = 1LL << 61;
        /*Pole pole(vRightSortedPoles[i].first, 0, 0, 0); 
        auto it = upper_bound(begin(vPoles), end(vPoles), pole);
        
        cout << (it - begin(vPoles)) << " " << it->x << " " << it->Left << " " << it->Right << endl;
        
        pole = Pole(vPoles[vRightSortedPoles[i].second].x - vPoles[vRightSortedPoles[i].second].Left, 0, 0, 0); 
        it = lower_bound(begin(vPoles), end(vPoles), pole);
        
        cout << (it - begin(vPoles)) << " " << it->x << " " << it->Left << " " << it->Right << endl << endl;*/
        int j = 0;
        while (j <= n && vPoles[j].x < vPoles[vRightSortedPoles[i].second].x - vPoles[vRightSortedPoles[i].second].Left) ++j;
        
        for (int k=j; k<=n; ++k)
        {
            minVal = min(minVal, costs[k] + vPoles[vRightSortedPoles[i].second].Cost);
        }

        int R = n;
        while (R >= 1 && vPoles[R].x > vRightSortedPoles[i].first) --R;
        
        cout << j << " " << R << endl;

        for (int k=1; k<=R; ++k)
        {
            costs[k] = min(costs[k], minVal);
        }
    }
    
    /*for (int i=0; i<=n; ++i)
    {
        cout << costs[i] << " ";
    }
    cout << endl;*/
    
    fout << costs[n] << "\n";

    return 0;
}