Cod sursa(job #1972071)

Utilizator cyg_LucaFlorinTanasescu Luca Florin cyg_LucaFlorin Data 21 aprilie 2017 18:54:52
Problema Heavy metal Scor 10
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.89 kb
#include <cstdio>
#include <algorithm>

using namespace std;

struct INTERVAL
{
    int x,y;
};
INTERVAL v[100005];
bool cmp(INTERVAL a,INTERVAL b)
{
    return a.x<b.x;
}
int main()
{
    freopen("heavymetal.in","r",stdin);
    freopen("heavymetal.out","w",stdout);
    int n,a,b,i,l,r,s;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        scanf("%d%d",&a,&b);
        v[i].x=a;
        v[i].y=b;
    }
    sort(v+1,v+n+1,cmp);
    l=v[1].x;
    r=v[1].y;
    s=r-l;
    for(i=2;i<=n;i++)
    {
        if(v[i].x>=r)
        {
            s=s+v[i].y-v[i].x;
            l=v[i].x;
            r=v[i].y;
        }
        else
        {
            if(v[i].y-v[i].x>r-l)
                {
                    s=s+v[i].y-v[i].x-r+l;
                    l=v[i].x;
                r=v[i].y;
                }
        }
    }
    printf("%d",s);
    return 0;
}