1. 程式人生 > >Codeforces Round #513 by Barcelona Bootcamp (rated, Div. 1 + Div. 2), problem: (D) Social Circles

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

        感覺比c題好想多了,因為環可以有隨便多個,自己和自己也可以成環,那就 記錄l,和 r 然後兩個陣列sort一下,按下標配對就好了,配對取大的那個,所有對求和,然後加上n個人就是答案,程式碼如下。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
inline int read(){
    int f = 1, x = 0;char ch = getchar();
    while (ch > '9' || ch < '0'){if (ch == '-')f = -f;ch = getchar();}
    while (ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}
    return x * f;
}
const int maxn = 1e5 + 10;
int l[maxn],r[maxn];
int main(){
    int n = read();
    for (int i = 0; i < n; ++i) {
        l[i] = read();
        r[i] = read();
    }
    sort(l,l+n);
    sort(r,r+n);
    LL ans = 0;
    for (int i = 0; i < n; ++i) {
        ans += max(l[i],r[i]);
    }
    cout << ans + n << endl;
    return 0;
}