-
Notifications
You must be signed in to change notification settings - Fork 0
/
P1496.cpp
50 lines (50 loc) · 937 Bytes
/
P1496.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
struct ship{
long long a;
long long b;
};
struct ship s[20002];
int main()
{
int n;
int length=0;
cin>>n;
long a1[20001],b1[20001];
for(int i=0;i<n;i++)
{
cin>>a1[i];
cin>>b1[i];
}
sort(a1,a1+n-1);
sort(b1,b1+n-1);
for(int i=0;i<n;i++)
{
long long a,b;
a=a1[i];
b=b1[i];
bool flag=false;
for(int j=0;j<length;j++)
{
if(b<s[j].a||a>s[j].b){
continue;
}
else {
s[j].a=min(a,s[j].a);
s[j].b=max(b,s[j].b);
flag=true;
break;
}
}
if(!flag){
s[length].a=a;
s[length].b=b;
length++;
}
}
long long num=0;
for(int i=0;i<length;i++){
num+=s[i].b-s[i].a;
}
cout<<num;
}