基础算法
作者:互联网
区间合并:
#include <bits/stdc++.h> using namespace std; typedef pair <int,int> pii; vector<pii>pos; void merge(vector<pii>&pos) { vector <pii>ans; sort(pos.begin(),pos.end()); int st=-1e9+10,ed=-1e9+10; for (auto ver:pos) { if (ed<ver.first) { if (st!=-1e9+10)ans.push_back({st,ed}); st=ver.first,ed=ver.second; } else { ed=max(ed,ver.second); } } if (st!=-1e9+10)ans.push_back({st,ed}); pos=ans; } int main() { int n; cin>>n; while (n--) { int l,r; cin>>l>>r; pos.push_back({l,r}); } merge(pos); cout<<pos.size()<<endl; //for (auto ver:pos)cout<<ver.first<<' '<<ver.second<<endl; return 0; }
标签:10,int,ed,基础,pos,merge,算法,1e9 来源: https://www.cnblogs.com/Mercury1988/p/16207754.html