Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2) D. Social Circles 【貪心】
D. Social Circles
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output
You invited nn guests to dinner! You plan to arrange one or more circles of chairs. Each chair is going to be either occupied by one guest, or be empty. You can make any number of circles.
Your guests happen to be a little bit shy, so the ii-th guest wants to have a least lili free chairs to the left of his chair, and at least riri free chairs to the right. The "left" and "right" directions are chosen assuming all guests are going to be seated towards the center of the circle. Note that when a guest is the only one in his circle, the lili chairs to his left and riri chairs to his right may overlap.
What is smallest total number of chairs you have to use?
Input
First line contains one integer nn — number of guests, (1⩽n⩽1051⩽n⩽105).
Next nn lines contain nn pairs of space-separated integers lili and riri (0⩽li,ri⩽1090⩽li,ri⩽109).
Output
Output a single integer — the smallest number of chairs you have to use.
Examples
input
Copy
3 1 1 1 1 1 1
output
Copy
6
input
Copy
4 1 2 2 1 3 5 5 3
output
Copy
15
input
Copy
1 5 6
output
Copy
7
Note
In the second sample the only optimal answer is to use two circles: a circle with 55 chairs accomodating guests 11 and 22, and another one with 1010 chairs accomodationg guests 33 and 44.
In the third sample, you have only one circle with one person. The guest should have at least five free chairs to his left, and at least six free chairs to his right to the next person, which is in this case the guest herself. So, overall number of chairs should be at least 6+1=7.
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 2e6 + 3;
int a[MAX], b[MAX];
int main(){
int n;
scanf("%d", &n);
for(int i = 0; i < n; i++){
scanf("%d%d", &a[i], &b[i]);
}
sort(a, a + n);
sort(b, b + n);
ll ans = 0;
for(int i = 0; i < n; i++){
ans += max(a[i], b[i]) + 1;
}
printf("%lld", ans);
return 0;
}