Cod sursa(job #2346572)

Utilizator AlexandruPaulSirbu Alex AlexandruPaul Data 17 februarie 2019 20:23:51
Problema Heavy metal Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
const int Maxx=1e5+1;
struct show{
    int st,fs;
}A[Maxx];
bool compx(show a,show b){
    if (a.fs==b.fs){
        return a.st<b.st;
    }
    return a.fs<b.fs;
}
ifstream fin("heavymetal.in");
ofstream fout("heavymetal.out");
int n;
int dp[Maxx];
//dp[i] - maximul ce a fost vazut pana la spectacolul i in sirul ordonat
int bs(int val,int dr){
    int st=1,mid,ret=0;
    while(st<=dr){
        mid=(st+dr)>>1;
        if (A[mid].fs<=val){
            st=mid+1;
        } else {
            dr=mid-1;
        }
    }
    return dr;
}
int main() {
    int i,next;
    fin>>n;
    for (i=1;i<=n;++i){
        fin>>A[i].st>>A[i].fs;
    }
    sort(A+1,A+n+1,compx);
    dp[1]=A[1].fs-A[1].st;
    for (i=2;i<=n;++i){
        next=bs(A[i].st,i-1);
        //cout<<next<<"\n";
        dp[i]=max(dp[i-1],dp[next]+A[i].fs-A[i].st);
    }
    fout<<dp[n];
    return 0;
}