ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 16936번 C++ 풀이
    백준 2018. 11. 18. 18:25

    벡터로 담아 정렬한 뒤에 하나씩 갱신합니다.


    #include <iostream>
    #include <algorithm>
    #include <queue>
    #include <climits>
    using namespace std;

    int buffer[1001];
    bool isVisited[1001];
    int main(){
    cin.tie(NULL);
    ios::sync_with_stdio(false);

    int n;
    cin >> n;
    vector<pair<int, int>> v;
    for (auto i=0; i<n; i++){
    int x,y;
    cin >> x >> y;

    v.push_back(make_pair(min(x,y),max(x,y)));
    }
    sort(v.begin(), v.end());
    long long totalLength = 0;
    // bool shouldThinkPrevious = false;
    for (auto i=1; i<v.size(); i++){
    // if( shouldThinkPrevious == false){
    // shouldThinkPrevious = true;
    // continue;
    // }
    if ( v[i-1].second < v[i].first){ //청산이 필요함
    totalLength += v[i-1].second - v[i-1].first;
    }else {
    v[i].first = min(v[i].first, v[i-1].first);
    v[i].second = max(v[i].second, v[i-1].second);
    }
    }
    totalLength += v[v.size()-1].second - v[v.size()-1].first;

    cout << totalLength;
    return 0;

    }



    '백준' 카테고리의 다른 글

    백준 16504 C++ 풀이  (0) 2018.11.18
    백준 16935번 C++ 풀이  (0) 2018.11.18
    백준 1325번 C++ 풀이  (0) 2018.11.18
    백준 14890번 C++ 풀이  (0) 2018.11.17
    백준 C++ 2161번 풀이  (0) 2018.11.16
Designed by Tistory.