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<cstdio>
using namespace std;

int main()
{
  int n;
  int num[100000][2];
  int maxa,minb;
  int i,j,k;
  int indexa,indexb;

  cin>>n;

  for(i=0;i<n;i++)
    cin>>num[i][0]>>num[i][1];

  for(k=0;k<n;k++){

    maxa=num[0][0];
    minb=num[0][1];
    indexa=indexb=0;
    for(i=1;i<n;i++){
      if(maxa<num[i][0]){
	maxa=num[i][0];
	indexa=i;
      }
      if(minb>num[i][1]){
	minb=num[i][1];
	indexb=i;
      }
    }
    if(maxa<=n-k+1 && n-k+1<=minb){
      cout<<n-k<<endl;
      break;
    }else if(maxa<=n-k+1){
      num[indexb][0]=0;
      num[indexb][1]=100001;
    }else if(minb>=n-k+1){
      num[indexa][0]=0;
      num[indexa][1]=100001;
    }
  }
  if(k>n-2)
    cout<<"0"<<endl;
}

C++: #include "bits/stdc++.h"

using namespace std;

static int member[100002];

void solve()
{
  int n, a, b;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    member[b]++;
    member[a - 1]--;
  }
  for(int i = 1e6 + 1; i > 0; i--) {
    if (i < 1e6 + 1) member[i] += member[i + 1];
    if(member[i] + 1 >= i) {
      cout << i - 1 << "\n";
      return;
    }
  }
}

int main()
{
  ios::sync_with_stdio(false);
  cin.tie();
  solve();
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,a,b;
int t[1000001];

int main(){
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d %d",&a,&b);
    t[a-1]++;
    t[b-1]--;
  }
  int ans=0;
  for(int i=1;i<=1000000;i++){
    t[i]+=t[i-1];
    if(i<=t[i])ans=i;
  }
  cout<<ans<<endl;
  return 0;
}

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

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int N;
	cin >> N;
	std::vector<int> frimin(N);
	std::vector<int> frimax(N);
	for (int i = 0; i < N; i++) {
		cin >> frimin[i] >> frimax[i];
	}

	std::sort(frimin.begin(), frimin.end());
	std::sort(frimax.begin(), frimax.end());

	int ret = 0;
	int count = 1;
	int imin = -1;
	int imax = -1;
	for (int f = 2; f < N+1 && f < frimax[N-1]; f++) {
		for (int i = imin+1; frimin[i] == f; i++) {
			count++;
			imin++;
		}
		for (int i = imax+1; frimax[i]+1 == f; i++) {
			count--;
			imax++;
		}

		if ( count >= f && count != 1 ) {
			ret = f-1;
		}
	}

	cout << ret << endl;
	return 0;
}

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

#define rep(i,x,y) for(int (i)=(x);(i)<(y);(i)++)
#define INF 1000000000

vector<int> A;
vector<int> B;

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

  vector<int> F(N+1,0);
  rep(i,0,N){
    int a,b;
    cin>>a>>b;
    a--;
    if(a<=N){
    F[a]++;
    }
    if(b<N){
      F[b]--;
    }
  }
  rep(i,0,N){
    F[i+1]+=F[i];
  }

  int ans=0;
  rep(i,0,N+1){
    if(F[i]>=i){
      ans=max(ans,i);
    }
  }

  cout<<ans<<endl;

  return 0;
}

C++: #include <iostream>
using namespace std;

int main()
{
  int N;
  cin >> N;
  int c[100003] = {};
  for(int i = 0; i < N; i++){
    int a, b;
    cin >> a >> b;
    c[a]++;
    c[b+1]--;
  }
  int ans = 0;
  int sum = 1;
  for(int i = 0; i < 100003; i++){
    sum += c[i];
    ans = (i <= sum) ? i : ans;
  }
  cout << ans - 1 << endl;
}

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;

int n;

bool check(VVI& table, int number) {
    // number ?????????????????????
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (table[i][0] <= number && table[i][1] >= number) {
            count++;
        }
    }
    if (count >= number - 1) {
        return true;
    } else {
        return false;
    }
}

int main(void) {
    cin >> n;
    VVI table(n, VI(2));
    for (int i = 0; i < n; i++) {
        cin >> table[i][0] >> table[i][1];
    }
    int left = 1;
    int right = n + 1;

/*
    while (left < right) {
        int middle = (left + right) / 2; // left??????
        if (check(table, middle) == true) {
            left = middle + 1;
        } else {
            right = middle;
        }
    }
    cout << left - 2 << endl; // left???1?????§?????? + ???????????????
*/
    while (left < right) {
        int middle = (left + right) / 2 + 1; // right??????
        if (check(table, middle) == true) {
            left = middle;
        } else {
            right = middle - 1;
        }
    }
    cout << left - 1 << endl; // ???????????????

    return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, a, n) for(int i = a; i < n; i++)
#define REP(i, n) rep(i, 0, n)
#define repb(i, a, b) for(int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define int long long
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
using namespace std;
typedef pair<int, int> P;
const int mod = 1000000007;
const int INF = 1e12;

int d[100010];

signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	int n;
	cin >> n;
	rep(i, 0, n){
		int a, b;
		cin >> a >> b;
		d[a]++;
		d[b + 1]--;
	}
	rep(i, 1, 100010){
		d[i] += d[i - 1];
	}
	int ans = 0;
	rep(i, 0, 100010){
		if(d[i] >= i - 1) chmax(ans, i - 1);
	}
	cout << ans << endl;
}