Social Circles

来自codeforces

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.

inputCopy
3
1 1
1 1
1 1
outputCopy
6
inputCopy
4
1 2
2 1
3 5
5 3
outputCopy
15
inputCopy
1
5 6
outputCopy
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.

题意一个人开晚会要叫朋友但他的朋友很害羞 需要左右两边都有空位求最小需要多少椅子,
这个题是真的不好看出来是贪心,看出来是贪心后就非常简单了(好神奇)
只需要将每个人的左边和右边分别排序,每次只需将大的空椅子(多的空椅子可以包含少的空椅子)+1(因为客人也要坐)这样就可以得出答案还有注意数据的大小用longlong。

#include <cmath>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
int n,a[100009],b[1000009];
int main()
{
    long long int s=0;
    while(cin>>n)
    {
        s=0;
        for(int i=0;i<n;i++)
        {
            cin>>a[i]>>b[i];
        }
        sort(a,a+n);
        sort(b,b+n);
        for(int i=0;i<n;i++)
        {
            s+=max(a[i],b[i])+1;
        }
        cout<<s<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值