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 <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];
    }

    /*
    // linear search -> TLE
    for (int i = n + 1; i >= 1; i--) {
    if (check(table, i) == true) {
    cout << i - 1 << endl;
    return 0;
    }
    }
     */

    int left = 1;
    int right = n + 1;

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

    return 0;
}

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

int fav[100005];

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

	memset(fav, 0, sizeof(fav));
	for(int i=0; i<N; i++)
	{
		int l,r;
		cin >> l >> r;
		fav[l]++;
		fav[r]--;
	}

	int res=0, mem=1;
	for(int i=1; i<=N+1; i++) {
		mem+=fav[i];
		if(mem >= i) res = i;
	}

	cout << res-1 << endl;
}

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

int N;
int D[100005];
int main(){
  cin >> N;
  for(int i=0;i<N;i++){
    int a,b; cin >> a >> b;
    D[a]++; D[b+1]--;
  }
  D[0]++;

  int res = 0;
  for(int i=1;i<=N;i++){
    D[i] = D[i]+D[i-1];
    if( D[i] >= i ) res = i;
  }
  cout << res-1 << endl;
}

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

using namespace std;
	

int main(){
	int N;
	vector<pair<int,int>> friends;
	vector<int> humans;
	vector<int> nokemono;

	//INPUT
	cin >> N;
	for(int i=0;i<N;i++){
		int a, b;
		cin >> a >> b;
		friends.push_back(make_pair(a,b));
	}
	//UPCOUNT & DOWNCOUNT
	for(int i=0;i<=N+1;i++){
		int count = 0;
		int minn = 999999;
		for(auto range:friends){
			if(i >= range.first && i <= range.second){
				count++;
				if(range.second < minn)
					minn = range.second;
			}
		}
		count++;
		humans.push_back(count);
		if(minn == 999999) minn = 0;
		nokemono.push_back(minn);
		count = 0;
	}

	for(int i=0;i<=N+1;i++){
		if(humans[i] < nokemono[i]) nokemono[i] = humans[i];
	}
	
	for(int i=N+1;i>=0;i--){
		//cout << i << ":" << humans[i] << "," << nokemono[i] << endl;
	}	
	
	//bool flag = true;
	for(int i=N+1;i>0;i--){
		if(humans[i] < i){
			//cout << i << endl;
			nokemono[i] = 0;
		}
		//if(nokemono[i] < humans[i]) nokemono[i] = 0;
		//cout << i << ":" << humans[i] << "," << nokemono[i] << endl;
		//cout << i << "," << humans[i]-1 << endl;
		//flag = false;
		//break;
	}
	
	/*if(flag)
		cout << 0 << endl;*/
	

	int maxn = 0;
	for(int i=0;i<=N+1;i++){
		if(nokemono[i] > maxn) maxn = nokemono[i];
	}
	
	cout << max(0,maxn-1) << endl;
	return 0;
}

C++: #include <bits/stdc++.h>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;

template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline void print_vec(const std::vector<T> &v) { for (int i = 0; i < v.size(); ++i) {  if (i != 0) {std::cout << " ";} std::cout << v[i];} std::cout << "\n"; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }
template<class T> inline double euclidean_distance(T y1, T x1, T y2, T x2) { return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); }
template<class T> inline double manhattan_distance(T y1, T x1, T y2, T x2) { return abs(x1 - x2) + abs(y1 - y2); }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy4 = { 0, 1, 0, -1 }, dx4 = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）
const std::vector<int> dy8 = { 0, -1, 0, 1, 1, -1, -1, 1 }, dx8 = { 1, 0, -1, 0, 1, 1, -1, -1 };

using namespace std;


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

	int N;
	cin >> N;

	vector<int> dp(100100, 0);
	FOR(i, 0, N) {
	    int a, b;
	    cin >> a >> b;
	    dp[a] += 1;
	    dp[b + 1] -= 1;
	}

	FOR(i, 0, dp.size() - 1) {
	    dp[i + 1] += dp[i];
	}

	int ans = 0;
	FOR(i, 0, dp.size()) {
	    if (dp[i] + 1 >= i) {
            ans = max(ans, i);
        }
	}
	print(ans);

	return 0;
}

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

int main() {
    int N;
    cin >> N;
    vector<int> V(N + 5);
    for (int i = 0; i < N; i++) {
        int a, b; cin >> a >> b;
        V[a]++;
        V[b+1]--;
    }

    int res = 0;
    for (int i = 0; i <= N; i++) {
        V[i + 1] += V[i];
        if (i <= V[i] + 1) res = i;
    }

    cout << res - 1 << endl;

    return 0;
}