BOJ 2836 수상 택시

Jmnote (토론 | 기여)님의 2024년 1월 31일 (수) 17:12 판 (새 문서: ==개요== {{BOJ |단계= 46 |분류1= 정렬 |분류2= 스위핑 }} ==C++== <syntaxhighlight lang='cpp'> #include <bits/stdc++.h> using namespace std; #define ll long long #define...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

BOJ 2836 수상 택시

2 C++[ | ]

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define PAIR pair<int, int>

int N, M;
vector<PAIR> v;

bool cmp(PAIR a, PAIR b) {
    if (a.second == b.second) return a.first > b.first;
    return a.second < b.second;
}

void solve() {
    sort(v.begin(), v.end(), cmp);
    ll res = M;
    PAIR pos = make_pair(1e9, 1e9);
    for (int i = v.size() - 1; i >= 0; i--) {
        if (pos.first <= v[i].first && v[i].second <= pos.second) continue;
        if (v[i].first < pos.first && pos.first <= v[i].second) {
            pos.first = v[i].first;
        } else {
            res += (pos.second - pos.first) * 2;
            pos = v[i];
        }
    }
    res += (pos.second - pos.first) * 2;
    cout << res;
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> N >> M;
    int a, b;
    for (int i = 0; i < N; i++) {
        cin >> a >> b;
        if (a > b) v.push_back({b, a});
    }
    solve();
}
문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}