Pagini recente » Cod sursa (job #285008) | Cod sursa (job #2012694) | Cod sursa (job #477072) | Cod sursa (job #2012670) | Cod sursa (job #1207044)
#include <cstdio>
#include <algorithm>
using namespace std;
struct BAND
{
int left;
int right;
};
BAND bands[10001];
long long best[10001];
int N;
int BinSearch(int POS)
{
int Begin, End, Mid, Closest(0);
Begin = 0; End = N;
while(Begin <= End)
{
Mid = (Begin+End)/2;
if(bands[Mid].right <= bands[POS].left)
{
Closest = Mid;
Begin = Mid+1;
}
if(bands[Mid].right > bands[POS].left) End = Mid-1;
}
return Closest;
}
bool SortRight(BAND A, BAND B) { return A.right < B.right; }
int main()
{
FILE *in = fopen("heavymetal.in", "r");
FILE *out = fopen("heavymetal.out", "w");
int i, pos, x, y;
fscanf(in, "%d", &N);
for(i=1; i<=N; i++)
{
fscanf(in, "%d %d", &x, &y);
bands[i].left = x;
bands[i].right = y;
}
sort(&bands[1], &bands[N+1], SortRight);
best[1] = bands[1].right - bands[1].left;
for(i=2; i<=N; i++)
{
best[i] = best[i-1];
pos = BinSearch(i);
best[i] = max(best[i], best[pos] + bands[i].right - bands[i].left);
}
fprintf(out, "%lld\n", best[N]);
return 0;
}