Skip to content

Instantly share code, notes, and snippets.

@overnew
Created November 7, 2020 12:54
Show Gist options
  • Save overnew/acd68ccb94886e0e050cf48899181d41 to your computer and use it in GitHub Desktop.
Save overnew/acd68ccb94886e0e050cf48899181d41 to your computer and use it in GitHub Desktop.
[BaekJoon]_2170_선 긋기
#include <iostream>
#include<algorithm>
using namespace std;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int N;
pair<int,int> points[1000000];
cin>>N;
int first,second;
int sum_length=0;
for(int i=0; i<N ; ++i){
cin>>first>>second;
points[i].first= first;
points[i].second = second;
}
sort(points, points+N);
int start_point = points[0].first, end_point = points[0].second;
for(int i=1; i<N ; ++i){
if(points[i].first > end_point){
sum_length += end_point - start_point;
start_point = points[i].first;
end_point = points[i].second;
}else if(points[i].second > end_point){
end_point = points[i].second;
}
}
sum_length += end_point - start_point;
cout<<sum_length<<'\n';
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment