Cod sursa(job #1981984)

Utilizator andreiutu111Noroc Andrei Mihail andreiutu111 Data 17 mai 2017 14:34:21
Problema Heavy metal Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.73 kb
#include<fstream>
#include<algorithm>
using namespace std;
struct spec{
    int x1,y1;
}A[100001];
int N,S,i,j,o;
bool cmp(spec x,spec y){
    if(x.y1>y.y1)return false;
    if(x.y1==y.y1 && x.x1>y.x1)return false;
    return true;
}
int main()
{
    freopen("heavymetal.in","r",stdin);
    freopen("heavymetal.out","w",stdout);
    scanf("%d",&N);
    for(i=1;i<=N;++i)
        scanf("%d%d",&A[i].x1,&A[i].y1);
    sort(A+1,A+N+1,cmp);
    o=1;
    while(A[o].x1==A[o+1].x1 && o<N)++o;
    S+=A[o].y1-A[o].x1;
    for(i=o+1;i<=N;++i){
        j=i;
        while(A[j].x1==A[j+1].x1 && j<N)++j;
        if(A[o].y1<=A[j].x1)
            S+=A[j].y1-A[j].x1,o=j;
        i=j;
    }
    printf("%d",S);
    return 0;
}