Cod sursa(job #493936)

Utilizator GheorgheMihaiMihai Gheorghe GheorgheMihai Data 19 octombrie 2010 22:37:41
Problema Stalpi Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.34 kb
#include <stdio.h>

#include <algorithm>

using namespace std;

int n;
int v[100002], c[100002], st[100002], dr[100002], o[100002];
long long aib[100002];

inline int cmp (int i, int j)
{
	return st[i] < st[j];
}

inline int lsb (int x) {return x & -x;}
inline int min (int a, long long b) {return a < b ? a : b;}

void update (int x, long long y)
{
	while (x)
	{
		aib[x] = min (aib[x], y);
		x -= lsb (x);
	}
}

long long query (int x)
{
	if (!x)
		return 0;
	long long sol = (long long)1 << 62;
	while (x <= n)
	{
		sol = min (sol, aib[x]);
		x += lsb (x);
	}
	return sol;
}

int cautare (int x)
{
	int st = 1, dr = n, m, sol = 0;
	while (st <= dr)
	{
		m = (st + dr) >> 1;
		if (x >= v[m])
		{
			st = m + 1;
			sol = m;
		}
		else
			dr = m - 1;
	}
	return sol;
}

int main ()
{
	freopen ("stalpi.in", "r", stdin);
	freopen ("stalpi.out", "w", stdout);
	
	scanf ("%d", &n);
	
	int i, cost;
	for (i = 1; i <= n; i ++)
	{
		scanf ("%d %d %d %d", &v[i], &c[i], &st[i], &dr[i]);
		st[i] = v[i] - st[i];
		dr[i] = v[i] + dr[i];
		o[i] = i;
		aib[i] = (long long) 1 << 62;
	}
	sort (v + 1, v + n + 1);
	sort (o + 1, o + n + 1, cmp);
	
	for (i = 1; i <= n; i ++)
	{
		cost = query (cautare (st[o[i]])) + c[o[i]];
		update (cautare (dr[o[i]]), cost);
	}
	
	printf ("%d\n", aib[n]);
	return 0;
}