Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2) D

D. Social Circles
time limit per test
2 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

You invited n

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 i
-th guest wants to have a least li free chairs to the left of his chair, and at least ri 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 li chairs to his left and ri

chairs to his right may overlap.

What is smallest total number of chairs you have to use?
Input

First line contains one integer n
— number of guests, (1⩽n⩽105

).

Next n
lines contain n pairs of space-separated integers li and ri (0⩽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 5
chairs accomodating guests 1 and 2, and another one with 10 chairs accomodationg guests 3 and 4

.

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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 1000005
#define inf 0x3f3f3f3f
#define INF 0x7fffffff
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const int mod = 10000007;
#define Mod 20100403
#define sq(x) (x)*(x)
#define eps 1e-10
const int N = 1505;

inline int rd() {
	int x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

ll cal(ll x) {
	ll ans = 0;
	while (x) {
		ans += (x % 10);
		x /= 10;
	}
	return ans;
}

int a[maxn], b[maxn];

int main()
{
	//ios::sync_with_stdio(false);
	int n; rdint(n);
	for (int i = 1; i <= n; i++)rdint(a[i]), rdint(b[i]);
	ll ans = (ll)n;
	sort(a + 1, a + 1 + n); sort(b + 1, b + 1 + n);
	for (int i = 1; i <= n; i++) {
		ans += (ll)max(a[i], b[i]);
	}
	cout << ans << endl;
}

猜你喜欢

转载自blog.csdn.net/qq_40273481/article/details/82940247