AcWing 3263. 买菜
原题链接
简单
作者:
把这题Ac了
,
2024-12-05 20:10:37
,
所有人可见
,
阅读 1
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair<int,int> PII;
const int N = 2010;
struct node{
int l,r;
}t1[N],t2[N];
int n;
bool cmp(node &a,node &b){
if(a.l != b.l) return a.l < b.l;
return a.r < b.r;
}
int main(){
cin >> n;
for(int i = 0;i < 2 * n;i++) cin >> t1[i].l >> t1[i].r;
sort(t1,t1 + 2 * n,cmp);
vector<PII> res;
int st = -2e9,ed = st;
for(int i = 0;i < 2 * n;i++){
int tl = t1[i].l,tr = t1[i].r;
if(tl > ed){
st = tl,ed = tr;
}else{
if(tr <= ed){
res.push_back({tl,tr});
st = tl;
}else{
res.push_back({tl,ed});
st = ed,ed = tr;
}
}
}
int cnt = 0;
for(auto t : res)
cnt += t.second - t.first;
cout << cnt << endl;
return 0;
}