Created
November 7, 2020 12:54
-
-
Save overnew/acd68ccb94886e0e050cf48899181d41 to your computer and use it in GitHub Desktop.
[BaekJoon]_2170_선 긋기
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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