b966. 3. 線段覆蓋長度 - 高中生程式解題系統

本文最後更新於:2024年9月20日 下午

Zerojudge
解題紀錄

b966. 3. 線段覆蓋長度 - 高中生程式解題系統

Zerojudge-b966-4.cpp

// Author : ysh
// 2024/04/10 Wed 21:24:51
#include<bits/stdc++.h>
using namespace std;
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n;cin>>n;
    vector<pair<int,int>>f(n);
    for(auto &i : f) cin>>i.first>>i.second;

    sort(f.begin(),f.end(),[] (pair<int,int>&a,pair<int,int>&b) {
        return a.first < b.first;
    });
    int ans = 0;
    int r = f.front().first - 1;
    for(auto &i : f) {
        ans = ans + max(0,(i.second - max(i.first,r)));
        r = max(r,i.second);
    }
    cout<<ans;
    return 0;
}

b966. 3. 線段覆蓋長度 - 高中生程式解題系統
http://mysh212.github.io/algosolution/Zerojudge-b966-4.cpp/
作者
ysh
發布於
2024年4月10日
更新於
2024年9月20日
許可協議