[BOJ] 2170번 : 선 긋기(C++)

김영한·2021년 4월 16일
0

알고리즘

목록 보기
48/74

문제 링크 : 백준 2170번

[문제 접근]

아우으 우아으이야!!와 똑같은 문제이다.

[소스 코드]

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;
int n;
vector<pair<int, int> > arr;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for(int i=0 ; i<n ; i++) {
        int a, b;
        cin >> a >> b;
        arr.push_back(make_pair(a,b));
    }
    sort(arr.begin(), arr.end());
    int ans=0;
    int start = arr[0].first, end = arr[0].second;
    for(int i=1 ; i<n ; i++) {
        int s = arr[i].first, e = arr[i].second;
        if(end>=s) {
            if(end<e) {
                end = e;
            } 
        } else {
            ans += end-start;
            start = s, end = e;
        }
    }
    ans += end-start;
    cout << ans << "\n";

    return 0;
}

0개의 댓글