Go: // A Way to Invite Friends(友だちの誘い方)
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331&lang=ja
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"strconv"
)

const N_MAX = 1000001

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n := nextInt(sc)
	allow_list := make([]int, N_MAX+100)
	nMax := 0

	for i := 0; i < n; i++ {
		a, b := nextInt(sc), nextInt(sc)
		allow_list[a] += 1
		allow_list[b+1] += -1
		nMax = int(math.Max(float64(nMax), float64(b)))
	}

	for i := 2; i <= N_MAX; i++ {
		allow_list[i] += allow_list[i-1]
	}

	res := 0
	for i := 2; i <= nMax; i++ {
		if i-1 <= allow_list[i] { // i人誘う時に行きたい人の人数を超えないか(自分を含めて)
			res = i - 1 // 自分は除く
		}
	}

	fmt.Println(res)
}

func nextInt(sc *bufio.Scanner) int {
	sc.Scan()
	i, err := strconv.Atoi(sc.Text())
	if err != nil {
		panic(err)
	}
	return i
}

C++: #include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

class Sum
{
    int n;
    vector<int> data;
    void query(int a, int b, int k, int l, int r, int x){
        if(r < a || b < l)
            return;
        if(a <= l && r <= b)
            data[k] += x;
        else{
            query(a, b, k*2+1, l, (l+r)/2, x);
            query(a, b, k*2+2, (l+r+1)/2, r, x);
        }
    }
public:
    Sum(int n0){
        n = 1;
        while(n < n0)
            n *= 2;
        data.assign(2*n-1, 0);
    }
    void add(int a, int b, int x){
        query(a, b, 0, 0, n-1, x);
    }
    int get(int k){
        k += n-1;
        int ret = data[k];
        while(k > 0){
            k = (k - 1) / 2;
            ret += data[k];
        }
        return ret;
    }
};

int main()
{
    int n;
    cin >> n;

    Sum sum(n+1);
    for(int i=0; i<n; ++i){
        int a, b;
        cin >> a >> b;
        sum.add(a, b, 1);
    }

    int i = n+1;
    for(;;){
        if(sum.get(i) >= i-1){
            cout << (i-1) << endl;
            break;
        }
        -- i;
    }

    return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

#define INF 1e09

using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::fixed;
using std::pair;
using std::make_pair;
using std::min;
using std::max;
using std::string;
using std::vector;
using std::list;
using std::map;
using std::set; // insert erase find count
using std::queue;
using std::priority_queue;
using std::stack;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, pair<int, int> > PIII;

#define MAX 1e05+1

int main(void) {
    int n;
    cin >> n;
    int start;
    int end;
    VI table(MAX + 1, 0);

    for (int i = 0; i < n; i++) {
        cin >> start >> end;
        start--;
        end--;
        for (int j = start; j <= end; j++) {
            table[j]++;
        }
    }
    /*
    for (int i = 0; i < 10; i++) {
        cout << i << ", " << table[i] << endl;
    }
    */
    for (int i = MAX; i >= 0; i--) {
        if (i <= table[i]) {
            cout << i << endl;
            break;
        }
    }
    return 0;
}

C++: #include<cstdio>

using namespace std;

const int MAX = 100005;
int res[MAX];

int main(){

  int n;
  scanf("%d",&n);

  while(n--){
    int a,b;
    scanf("%d %d",&a,&b);
    res[a-1]++;
    res[b]--;
  }

  int now = 0;
  int ans = 0;
  for(int i = 0; i < MAX; i++){
    now += res[i];
    if(now >= i) ans = i;
  }

  printf("%d\n",ans);
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

const int MAXN = 100005;
int N;
int a[MAXN], b[MAXN];

int main() {
  while (cin >> N) {
    map<int, int> m;
    for (int i = 0; i < N; ++i) {
      cin >> a[i] >> b[i];
      m[a[i]]++;
      m[b[i]]--;
    }
    int res = 1, num = 1;
    for (map<int, int>::iterator it = m.begin();
         it != m.end(); ++it) {
      num += it->second;
      if (num >= it->first) {
        res = max(res, it->first);
      }
    }
    cout << res - 1 << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <algorithm>

#define rep(i,n) for (int i = 0; i < (n); ++i)

int n, a, b, friends[100005], ans, maxn, ansi;

int main() {
    std::cin.tie(0);
    std::ios::sync_with_stdio(false);

    std::cin >> n;
    rep(i,n) {
        std::cin >> a >> b;
        if (a > n+1) {
            maxn++;
            continue;
        } else if (b > n+1-maxn) {
            b = n+1-maxn;
        }
        for (int i = a; i <= b; ++i) friends[i]++;
    }
    //for (int i = 0; i <= n+1; ++i) std::cout << friends[i] << std::endl;
    rep(i,n+1) {
        if (ansi < friends[i]) {
            ans = i-1;
        }
    }
    if (ans < 0) ans = 0;
    std::cout << ans << std::endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
  const int MAX = 100001;
  int n, a, b;
  int dp[MAX+1] = {};
   
  cin >> n;
  for(int i=0; i<n; i++) {
    cin >> a >> b;
    dp[a] ++;
    dp[b+1] --;
  }
 
  for(int i=2 i<MAX; i++) {
    dp[i+1] += dp[i];
  }
   
  int mx = 0;
  for(int i=2; i<MAX; i++) {
    if(dp[i]+1>=i) mx = i-1;
  }
  cout << mx << endl;
   
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

signed main(){
	int N=read();
	int a[N],b[N];
	int check[100050]={0};
	REP(i,N){
		a[i]=read();
		b[i]=read();
	}
	
	REP(i,N){
		check[a[i]] += 1;
		check[b[i]+1] -=1;
	}
	
	int sum=1;
	int ans=0;
	REP(i,N+2){
		sum += check[i];
		if(sum>=i) ans=i;
	}
	cout << ans-1 << endl;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
  int field[100001];
  int N;
  cin >> N;
  memset(field, 0, sizeof(field));
  int a, b;
  while(N--){
    cin >> a >> b;
    field[a]++;
    field[b]--;
  }

  int ans = 0, sum = 0;

  for(int i = 1 ; i < 100001 ; i++){
    sum += field[i];
    if(i <= sum+1) ans = i;
  }
  
  cout << ans-1 << endl;
  return 0;
}

C++: #include<iostream>
#include<algorithm>

using namespace std;

int main(){
  int N,table[100002]={};
  cin >> N;
  for(int i = 0 ; i < N ; i++){
    int a,b;
    cin >> a >> b;
    for(int i = a ; i <= b ; i++ ) table[i]++;
  }
  int res = 0;
  for(int i = 0 ; i <= N + 1 ; i++ ){
    if( ++table[i] >= i) res = i - 1;
  }
  cout << res << endl;
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<n;++i)

using namespace std;

typedef pair<int,int>P;

const int MAX_N = 100005;

int a[MAX_N],b[MAX_N];
int n;

bool possible(int u)
{
    int cnt = 1;
    rep(i,n){
        if(a[i] <= u + 1 && b[i] >= u + 1){
            cnt++;
            if(cnt >= u){
                return true;
            }
        }
    }
    return false;
}

int main()
{
	scanf("%d",&n);
	rep(i,n){
		scanf("%d%d",&a[i],&b[i]);
	}
    int h,l;
    l = 1,h = n+2;
    while(1){
        int mid = (l + h) / 2;
        if(mid == l){
            printf("%d\n",mid-1);
            break;
        }
        if(possible(mid)){
            l = mid;
        }else{
            h = mid;
        }
    }
	return 0;
}