Cod sursa(job #973600)

Utilizator claudiumihailClaudiu Mihail claudiumihail Data 14 iulie 2013 20:03:49
Problema Stalpi Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.74 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(- (1 << 30), 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(vPoles[vRightSortedPoles[i].second].x - vPoles[vRightSortedPoles[i].second].Left, 0, 0, 0);
        int L = lower_bound(begin(vPoles), end(vPoles), pole) - begin(vPoles) - 1;
        
        pole = Pole(vRightSortedPoles[i].first + 1, 0, 0, 0);
        int R=lower_bound(begin(vPoles), end(vPoles), pole) - begin(vPoles) - 1;
        
        for (int k=L; k<=n; ++k)
        {
            minVal = min(minVal, costs[k] + vPoles[vRightSortedPoles[i].second].Cost);
        }
        
        //cout << L << " " << 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;
}