#28695: CPP


11030067@mail.hpsh.tp.edu.tw (和平110級鄧雨珊)

學校 : 臺北市立和平高級中學
編號 : 163096
來源 : [61.64.210.174]
最後登入時間 :
2022-10-23 16:54:59
b966. 3. 線段覆蓋長度 -- 2016年3月apcs | From: [219.85.177.13] | 發表日期 : 2021-12-27 22:29

#include <iostream>
#include <algorithm>
#define N 1000001
using namespace std;
typedef pair<int, int> pii;
pii a[10001];

bool cmp(pii x, pii y){
if (x.first==y.first){
return x.second<y.second;
}
else {
return x.first<y.first;
}
}

int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int n;
while (cin >> n){
for (int i=0; i<n; i++){
cin >> a[i].first >> a[i].second;
}
sort(a, a+n, cmp);
int sum=a[0].second-a[0].first;
int R=a[0].second;
for (int i=1; i<n; i++){
if (a[i].second>R){
sum+=(a[i].second-a[i].first)-(R-a[i].first)*((R-a[i].first)>0);
R=a[i].second;
}
}
cout << sum <<endl;
}
}
 
ZeroJudge Forum