Files
Data-Structure/Algorithm/Sort/P1496 火烧赤壁.cpp
2025-09-21 12:21:50 +08:00

35 lines
721 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
struct cmp{
bool operator()(const pair<int, int>& a, const pair<int, int>& b){
return a.first < b.first;
}
};
int main(){
int n;
cin >> n;
vector<pair<int, int>> dp(n);
//ÊäÈë
for(int i = 0; i < n; i++){
cin >> dp[i].first >> dp[i].second;
}
//°´ÕÕdp.firstÅÅÐò
sort(dp.begin(), dp.end(), cmp());
vector<pair<int, int>> merge;
for(auto [l, r] : dp){
if (merged.empty() || merged.back().second < l)
merged.push_back({l,r});
else
merged.back().second = max(merged.back().second, r);
}
int sum = 0;
for(auto [l, r] : merge){
sum += r - l;
}
cout << sum << endl;
return 0;
}