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 <algorithm>

int imos[100010];

using namespace std;


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

  int ans = 0;
  imos[0] = 1;
  for (int i = 1; i < 100010; i++) {
    imos[i] += imos[i - 1];
    if (imos[i] >= i) {
      ans = max(ans, imos[i]);
    }
  }

  cout << ans - 2 << endl;

  return 0;
}

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

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define between(a,x,b) ((a)<=(x)&&(x)<=(b))
using namespace std;

int main(){
  int n;
  scanf("%d", &n);
  int a, b;
  vector<pair<int, int> >memo(n);
  rep(i, n){
    scanf("%d%d", &a, &b);
    memo[i] = make_pair(b, a);
  }
  sort(memo.begin(), memo.end());

  int res = 1;
  bool f = true;
  while(f){
    f = false;
    rep(i, n){
      if(res + 1 >= memo[i].second && res + 1 <= memo[i].first){
	res++;
	printf("%d %d\n", memo[i].second, memo[i].first);
	memo[i].second = 200000;
	f = true;
	break;
      }
    }
  }
  printf("%d\n", res-1);
  return 0;
}

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

int s[100001] = {0};

int main(){
	int n;
	cin >> n;
	for(int i=0 ; i < n ; i++ ){
		int a, b;
		cin >> a >> b;
		s[a]++, s[b+1]--;
	}
	
	for(int i=1 ; i < 100001 ; i++ ){
		s[i] += s[i-1];
	}
	int ans = 1;
	for(int i=1 ; i < 10001 ; i++ ){
		if( i <= s[i] ) ans = i;
	}
	ans--;
	cout << ans << endl;
}

C++: #include <iostream>
#include <algorithm>
#define N 100000
int a[N],b[N],c[N];
using namespace std;
int main( void )
{
  int i,j,k,n,d=0,ret=0;
  cin >> n;
  for( i=0; i<n; i++ )
    cin >> a[i] >> b[i];
  sort( a, a+n );
  sort( b, b+n );
  for( i=2,j=0,k=0; i<=n+1; i++ ){
    while( a[j] == i ){ j++; d++; }
    while( b[k] == i-1 ){ k++; d--; }
    if( i-1 <= d ) ret = i-1;
  }
  cout << ret << endl;
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define RFOR(i,m,n) for(int i=m;i>=n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265

P a[1000000];
int n;

int search(int m){
  int c=1;
  rep(i,n){
    if(a[i].first<=m&&m<=a[i].second)
      c++;
  }

  if(c>=m) return true;

  return false;
}


int main(){
  cin>>n;
  rep(i,n){
    int b,c;
    cin>>b>>c;
    a[i]=P(b,c);
  }

  int r=0,l=inf;

  while(l-r!=1){
    int m = (r+l)/2;
    if(search(m))
      r = m;
    else
      l = m;
  }

  cout<<r-1<<endl;

}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep1(i, n) for (int i = 1; i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
using namespace std;
typedef long long LL;
const int INF = 999999999;

int main(){
    int n;
    cin >> n;
    vector<int> imos(101010, 0);
    rep(i, n){
        int a, b;
        cin >> a >> b;
        a--; b--;
        imos[a]++; imos[b+1]--;
    }
    int ans = 0;
    rep1(i, n+1){
        imos[i] += imos[i-1];
        if(i <= imos[i]) ans = max(ans, i);
    }
    cout << ans << endl;
}

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

using namespace std;

int main(){
	int n;
	cin >> n;
    vector<pair<int , int> > invert(n);

    int nmax=-10,nmin=(int)1e7;
    for(int i = 0; i < n; i++){
        int a , b;
        cin >> a >> b;
        invert[i].first = b;
        invert[i].second = a;

        nmin = min(nmin,a);
        nmax = max(nmax,b);
    }
    sort(invert.begin() , invert.end());
    nmax = min(nmax , n);
    int ans=0;

    for(int i = nmax; i >= nmin; i--){
        vector<pair<int , int> >::iterator up;
        pair<int , int> mp(i+1,0);
        up = lower_bound(invert.begin() , invert.end() , mp);
        int lower = distance(invert.begin() , up);
        int c = 0;
//        cout << invert[lower].first << " " << invert[lower].second << endl;
        for(int j = lower; j < invert.size(); j++){
            if(invert[j].second <= i+1){
                c++;
            }
        }
        if(c >= i){
            ans = i;
            break;
        }
    }
    cout << ans << endl;

    return 0;
}

C++: #include <iostream>
using namespace std;
int n;
int a[100000];
int b[100000];
int count;

int main(){
	cin >>n;
	for(int i=0;i<n;i++){
		cin>>a[i]>>b[i];
	}
	int sum;
	for(sum=n+1;sum>0;sum--){
		count=1;
		for(int i=0;i<n;i++){
			if(a[i]<=sum && sum<=b[i])count++;
			if(count==sum)break;
		}
		if(count==sum)break;
	}
	cout<<sum-1 <<endl;
}

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

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> v(100000 + 2, 0);
    for (int i = 0; i < n; ++i) {
        int a;
        int b;
        cin >> a >> b;
        for (int j = a; j <= b; ++j) {
            v[j] += 1;
        }
    }
    int max_{0};
    for (int i = 0; i < v.size(); ++i) {
        if (v[i] != 0 && (i - 1) <= v[i]) {
            max_ = i;
        }
    }
    if(max_ != 0) {
        cout << (max_ - 1) << endl;
    } else {
        cout << max_ << 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);
    if (s-t >= i-1) {
      ans = i-1;
      break;
    }
  }
  cout << ans << endl;
}

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

using namespace std;

#define FOR(i,k,n) for(int i=(k); i<(int)n; ++i)
#define REP(i,n) FOR(i,0,n)
#define FORIT(i,c) for(__typeof((c).begin())i=(c).begin();i!=(c).end();++i)
typedef pair<int,int> P;

int main(){
  int n;
  while(cin>>n){
    vector<int> a(100010);
    vector<int> b(100010);
    REP(i,n){
      int t1,t2;
      cin>>t1>>t2;
      a[t1]++;
      b[t2]++;
    }
    int ans = 0;
    int member = 0;
    for(int i = 0; i <= n; i++){
      member += a[i+1];
      member -= b[i];
      if(member >= i)ans = i;
    }
    cout<<ans<<endl;
  }
  return 0;
}