社交圈 - 题目 - Daimayuan Online Judge
思路:我们能够想到,如果i,j是并列的,则l[i]与r[j]会有重合的部分,那么其实重合的部分越多越好,其实就是让l[i]与r[i]的差值越小越好,同时要让越小越好,即对于所有的差值的和越小越好,我们能够发现对于这种情况通过贪心能够知道如果两个数组都是有序的,那么这两个序列的差值和是最小的,同时我们发现我们一定能够构造出来一种解是满足这种匹配的
通过上面的证明我们能够知道一定可以构造出一种满足要求的解
#include<iostream>
#include<cstring>
#include<string>
#include<sstream>
#include<bitset>
#include<deque>
#include<cmath>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<set>
#include<unordered_map>
#include<ctime>
#include<cstdlib>
#define fi first
#define se second
#define i128 __int128
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<int,int> PII;
typedef pair<int,pair<int,int> > PIII;
const double eps=1e-7;
const int N=5e5+7 ,M=5e5+7, INF=0x3f3f3f3f,mod=1e9+7,mod1=998244353;
const long long int llINF=0x3f3f3f3f3f3f3f3f;
inline ll read() {ll x=0,f=1;char c=getchar();while(c<'0'||c>'9') {if(c=='-') f=-1;c=getchar();}
while(c>='0'&&c<='9') {x=(ll)x*10+c-'0';c=getchar();} return x*f;}
inline void write(ll x) {if(x < 0) {putchar('-'); x = -x;}if(x >= 10) write(x / 10);putchar(x % 10 + '0');}
inline void write(ll x,char ch) {write(x);putchar(ch);}
void stin() {freopen("in_put.txt","r",stdin);freopen("my_out_put.txt","w",stdout);}
bool cmp0(int a,int b) {return a>b;}
template<typename T> T gcd(T a,T b) {return b==0?a:gcd(b,a%b);}
template<typename T> T lcm(T a,T b) {return a*b/gcd(a,b);}
void hack() {printf("\n----------------------------------\n");}
int T,hackT;
int n,m,k;
int l[N],r[N];
void solve() {
n=read();
for(int i=1;i<=n;i++) l[i]=read(),r[i]=read();
ll res=0;
sort(l+1,l+1+n);
sort(r+1,r+1+n);
for(int i=1;i<=n;i++) res+=max(l[i],r[i]);
printf("%lld\n",res+n);
}
int main() {
// init();
// stin();
// scanf("%d",&T);
T=1;
while(T--) hackT++,solve();
return 0;
}