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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值