Cod sursa(job #2498013)

Utilizator andreighinea1Ghinea Andrei-Robert andreighinea1 Data 23 noiembrie 2019 13:27:28
Problema Heavy metal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.83 kb
#include <iostream>
#include <fstream>
#include <algorithm>
#define Nmax 100001

using namespace std;

ifstream f("heavymetal.in");
ofstream o("heavymetal.out");

struct form{
    int a,b;
}fo[Nmax];

bool cmp(form &f1,form &f2){
    return f1.b < f2.b;
}

int i,j,n,dp[Nmax],t,s,d,m,g;

int main()
{
    f >> n;
    for(i=1;i<=n;++i)
        f >> fo[i].a >> fo[i].b;
    sort(fo+1,fo+n+1,cmp);

    dp[1]=fo[1].b-fo[1].a;
    for(i=2;i<=n;++i){
        t=fo[i].b-fo[i].a;
        dp[i]=dp[i-1];

        s=1;
        d=i;
        g=0;
        while(s<=d){
            m=(s+d)>>1;
            if(fo[m].b<=fo[i].a){
                s=m+1;
                g=max(g,m);
            }
            else
                d=m-1;
        }
        dp[i]=max(dp[i],dp[g]+t);
    }
    o << dp[n];

    return 0;
}