takumi's blog

By takumi, history, 11 years ago, In English

include

include

include

include

include

include

include

using namespace std;

typedef struct node { long long int a,b ;

}node ;

int arrange(node a,node b) { if(a.a<b.a) { return 1 ;

}
else if(a.a==b.a)
{
    if(a.b<=b.b)
    {
       return 1;
    }
    else
    {
       return 0 ;
    }  
}
return 0 ;

}

int main() { int n ; cin>>n ;

vector<node> list(n);

for(int i=0;i<n;i++)
{
    cin>>list[i].a ;
    cin>>list[i].b ;
} 

sort(list.begin(),list.end(),arrange) ;

long long int ans = list[0].b;
for(int i=1;i<n;i++)
{

    if(list[i].b<ans)
    {
       ans= list[i].a ;
    }
    else if(list[i].b>=ans)
    {
       ans = list[i].b;

    }


}

cout<<ans<<endl;
return 0;

}

  • Vote: I like it
  • -12
  • Vote: I do not like it

»
11 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

What are the constraints? If n = 0 is possible then "long long int ans = list[0].b;" will result in a RTE.

»
11 years ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Comparator is wrong. It shouldn't return true for two equal objects. This has been discussed on Codeforces countless times.

Typical implementation would be:

bool cmp(node a, node b) {
    if (a.a != b.a) return a.a < b.a;
    return a.b < b.b;
}