Cod sursa(job #1936205)

Utilizator giotoPopescu Ioan gioto Data 22 martie 2017 22:07:05
Problema Stalpi Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.05 kb
#include <cstdio>
#include <algorithm>
#define INF 2000000000
using namespace std;

int n, d[100002];
struct da{
    int x, c, s, d;
}a[100002];
inline bool cmp(da x, da y){
    return x.x < y.x;
}
inline bool cmp2(da x, da y){
    if(x.s != y.s) return x.s < y.s;
    return x.d < y.d;
}
int main()
{
    freopen("stalpi.in","r", stdin);
    freopen("stalpi.out", "w", stdout);
    scanf("%d", &n);
    for(int i = 1; i <= n ; ++i)
        scanf("%d%d%d%d", &a[i].x, &a[i].c, &a[i].s, &a[i].d);
    sort(a + 1, a + n + 1, cmp);
    for(int i = 1; i <= n ; ++i){
        int x = a[i].x - a[i].s;
        int j = i;
        while(j > 1 && a[j - 1].x >= x) --j;
        a[i].s = j;
        x = a[i].x + a[i].d;
        j = i;
        while(j < n && a[j + 1].x <= x) ++j;
        a[i].d = j;
        d[i] = INF;
    }
    sort(a + 1, a + n + 1, cmp2);
    for(int i = 1; i <= n ; ++i)
        for(int j = a[i].s; j <= a[i].d ; ++j){
            d[j] = min(d[j], d[a[i].s - 1] + a[i].c);
        }
    printf("%d", d[n]);
    return 0;
}