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 <cstdio>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <climits>
#include <ctime>
#include <queue>
#include <stack>
#include <algorithm>
#include <list>
#include <vector>
#include <set>
#include <map>
#include <iostream>
#include <deque>
#include <complex>
#include <string>
#include <iomanip>
#include <sstream>
#include <bitset>
#include <valarray>
#include <iterator>
using namespace std;
typedef long long int ll;
typedef unsigned int uint;
typedef unsigned char uchar;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

#define REP(i,x) for(int i=0;i<(int)(x);i++)
#define FOR(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RFOR(i,c) for(__typeof((c).rbegin())i=(c).rbegin();i!=(c).rend();i++)
#define ALL(container) container.begin(), container.end()
#define RALL(container) container.rbegin(), container.rend()
#define SZ(container) ((int)container.size())
#define mp(a,b) make_pair(a, b)
#define UNIQUE(v) v.erase( unique(v.begin(), v.end()), v.end() );

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T> ostream& operator<<(ostream &os, const vector<T> &t) {
os<<"["; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"]"; return os;
}
template<class T> ostream& operator<<(ostream &os, const set<T> &t) {
os<<"{"; FOR(it,t) {if(it!=t.begin()) os<<","; os<<*it;} os<<"}"; return os;
}
template<class S, class T> ostream& operator<<(ostream &os, const pair<S,T> &t) { return os<<"("<<t.first<<","<<t.second<<")";}

const int INF = 1<<28;
const double EPS = 1e-8;
const int MOD = 1000000007;


int n;

main(){
	while(cin >> n){
		vector<int> im(100002);
		REP(i, n){
			int x, y;
			cin >> x >> y;
			im[x--] ++;
			im[y] --;
		}
		int ans = 0;
		REP(i, n+1){
			im[i+1] += im[i];
			if(im[i+1] > i) ans = i;
		}
//		cout << im << endl;
		cout << ans << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define int long long
#define double long double
#define INF 1e18
using namespace std;
int x[100100];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  for (int i = 0; i < 100100; i++) x[i]=0;
  int N; cin >> N;
  for (int i = 0; i < N; i++) {
    int a,b; cin >> a >> b;
    if (a > N+1) continue;
    for (int j = a; j <= min(b,N+1); j++) {
      x[j]++;
    }
  }
  int ans = 0;
  for (int i = 0; i <= N+1; i++) {
    if (x[i]+1 >= i) ans=max(ans,max(i,x[i]));
  }
  ans = max(0LL, ans-1);
  cout << ans << endl;
}

C++: #include <stdio.h>

int n, x, y, imos[100003], table[100001];

int main()
{
    scanf("%d", &n);
    
    for(int i = 0; i < n; i++)
    {
        scanf("%d", &x);
        scanf("%d", &y); y++;
        
        imos[x]++;
        imos[y]--;
    }
    
    for(int i = 1; i <= 100001; i++)
    {
        table[i] = table[i - 1] + imos[i];
    }
    
    int ret = 0;
    
    for(int i = 1; i <= 100001; i++)
    {
        if(i - 1 <= table[i])
        {
            ret = i;
        }
    }
    
    printf("%d\n", ret);
    
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct Data{int num,io;};
bool operator<(Data a,Data b){return a.num!=b.num?a.num<b.num:a.io<b.io;};
enum{IN=1,OUT=-1};

int main()
{
  int N,a,b;
  cin>>N;
  vector<Data>list(N);
  for(int i=0;i<N;i++){
    cin>>a>>b;
    Data d1={a,IN};
    list.push_back(d1);
    Data d2={b,OUT};
    list.push_back(d2);
  }
  sort(list.begin(),list.end());
  int sum=1,ans=0;
  for(int i=0;i<N*2;i++){
    sum+=list[i].io;
    if(sum>=list[i].num)ans=max(ans,list[i].num);
  }
  cout<<ans<<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=1; i<MAX; i++) {
    dp[i+1] += dp[i];
  }
   
  int mx = 0;
  for(int i=1; i<MAX; i++) {
    if(dp[i]>=i) mx = i-1;
  }
  cout << mx << endl;
   
  return 0;
}

C++: #include <vector>
#include <iostream>
#include <utility>
#include <algorithm>
#include <string>
#include <deque>
#include <queue>
#include <tuple>
#include <queue>
#include <functional>
#include <cmath>
#include <iomanip>
#include <map>
#include <set>
#include <numeric>
#include <unordered_map>
#include <unordered_set>
#include <complex>
#include <iterator>
#include <array>
#include <memory>
#include <stack>
#define vi vector<int>
#define vvi vector<vector<int> >
#define ll long long int
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vb vector<bool>
#define vc vector<char>
#define vs vector<string>
#define ld long double
#define INF 1e9
#define EPS 0.0000000001
#define rep(i,n) for(int i=0;i<n;i++)
#define loop(i,s,n) for(int i=s;i<n;i++)
#define all(in) in.begin(), in.end()
template<class T, class S> void cmin(T &a, const S &b) { if (a > b)a = b; }
template<class T, class S> void cmax(T &a, const S &b) { if (a < b)a = b; }
#define MAX 9999999
using namespace std;
typedef pair<int, int> pii;
typedef pair<double,double>pdd;
typedef pair<ll,ll>pll;
int main(){
    int imos[1001000]={0};
    int gotiusa; cin>>gotiusa;
    int range=0;
    vector<pii>v;
    rep(i,gotiusa){
        int tiya,rize; cin>>tiya>>rize;
        imos[tiya]++; imos[rize]--;
        cmax(range,rize+1);
        v.push_back(pii(tiya,rize));
    }
    rep(_,1001000)
    if(_)imos[_]+=imos[_-1];
    int ans=0;
    rep(i,v.size())
    if(v[i].first<=imos[v[i].first]&&v[i].second>=imos[v[i].second])
        ans++;
    cout<<ans<<endl;
    
}

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

int main(){
  int N, a, b, ans, s, t;
  cin >> N;
  vector<int> A, B;
  for (int i = 0; i < N; ++i) {
    cin >> a >> b;
    A.push_back(a);
    B.push_back(b);
  }
  sort(A.begin(),A.end());
  sort(B.begin(),B.end());
  ans = 0;
  vector<int>::iterator itr1, itr2;
  for (int i = N + 1; i > 0; --i) {
    itr1 = lower_bound(A.begin(),A.end(),i+1);
    itr2 = lower_bound(B.begin(),B.end(),i);
    s = distance(A.begin(), itr1);
    t = distance(B.begin(), itr2);
    cout << i << " " <<  s << " " << t << endl;
    if (s-t >= i-1) {
      ans = i-1;
      break;
    }
  }
  cout << ans << endl;
}