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 <stdio.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;


int main(){
	int n;
	int a[100001], b[100001];

	a[0] = 0; b[0] = 100001;
	cin >> n;
	int max = 0, min = 100001;
	for (int i = 0; i < n; ++i){
		cin >> a[i] >> b[i];
		if (a[i] < min) min = a[i];
		else if (b[i] > max) max = b[i];
	}

	int max_friends = 0;
	for (int i = min; i < max+1; ++i){
		int friends = 0;
		for (int j = 0; j < n; ++j){
			if (a[j] <= i && b[j] >= i){
				++friends;
//				cout << i << a[j] << b[j] << "OK" << endl;
			}
			else{
//				cout << i << a[j] << b[j] << "NG" << endl;

			}
		}
		if ((friends) == i){
			max_friends = i-1;
		}
	}

	cout << max_friends << endl;



	return 0;
}

C++: #include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <cmath>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#include <stdlib.h>
#include <stdio.h>
#include <bitset>
#include <cstring>
using namespace std;
#define FOR(I,A,B) for(int I = (A); I < (B); ++I)
#define CLR(mat) memset(mat, 0, sizeof(mat))
typedef long long ll;
int main()
{
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N; cin >> N;
  int imos[100005]; CLR(imos);
  FOR(i,0,N) {
    int a, b; cin >> a >> b;
    imos[a]++;
    imos[b+1]--;
  }
  int ans = 0;
  FOR(i,0,N+1) {
    imos[i+1] += imos[i];
    if(i >= 1 && imos[i] >= i - 1) {
      ans = max(ans, imos[i]);
    }
  }
  cout << ans << endl;
  return 0;
}

C++: #include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
#include <utility>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
 
using namespace std;
 
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define rep(i,n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(),(r).end()
#define rall(r) (r).rbegin(),(r).rend()
#define fi first
#define se second
 
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input
{
	int n;char c;char str[1024];string s;ll l;
	int nextInt(){scanf("%d", &n);return n;}
	ll nextLong(){scanf("%lld", &l);return l;}
	char nextChar(){scanf("%c", &c);return c;}
	string next(){scanf("%s", str);return string(str);}
	string nextLine(){getline(cin, s);return s;}
};
Input in;

int c[100100];

int main(){
	int n = in.nextInt();
	vi a(n), b(n);
	rep(i, n) a[i] = in.nextInt(), b[i] = in.nextInt();

	vector<pii> p;
	rep(i, n) p.pb({a[i], b[i]});
	sort(all(p));

	int ans = 0;

	int idx1 = 0, idx2 = 0;
	for(int i = 0; i < n + 1; i++){
		//i + 1 nin de ikeru tomodachi no kazu
		int idx = upper_bound(all(p), mp(i + 1, INF)) - p.begin();
		//c[x] := x nin ijou ha dame
		for(int j = idx1; j < idx; j++){
			if(p[j].se < i) c[i]++;
			c[p[j].se]++;
		}
		if(i > 0) c[i] += c[i-1];
		// i + 1 nin atumaranai
		if(i > idx) continue;
		ans = max(ans, i - c[i]);
	}

	cout<<ans<<endl;
}

C++: #include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
//#define INF (1LL<<59)

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

    int ans = 0;
    rep(i,n+1)
        if(imos[i]+1>i)
            ans = max(ans,min(imos[i],(int)i-1));
    
    cout<<ans<<endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
    int n;cin>>n;
    vector<int> k(100002);
    int ma=0;
    for(int i=0;i<n;i++){
        int a,b;cin>>a>>b;
        ma=max(ma,b);
        k[a-1]--;
        k[b]++;
    }
    int count=1;
    for(int i=ma;i>=0;i--){
        count+=k[i];
        if(count>=i){
            count=i;break;
        }
    }
    cout<<count-1<<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] --;
  }
 
  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]+1>=i) mx = max(mx, dp[i]);
  }
  cout << mx << endl;
   
  return 0;
}

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

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

struct data_t {
	int min;
	int max;

	bool operator<(const data_t& right) const {
		if ( min == right.min ) {
			return  ( ( max < right.max ) || ( max == right.max ) );
		}
		else {
			return ( min < right.min );
		}
	}
};

int main(void) {
	int N;
	cin >> N;
	std::vector<data_t> fri(N);
	int fmin = 100001;
	for (int i = 0; i < N; i++) {
		cin >> fri[i].min >> fri[i].max;
		if ( fri[i].min < fmin ) {
			fmin = fri[i].min;
		}
	}

	int ret = 0;
	int count;
	for (int f = fmin; f < N+2; f++) {
		count = 1;
		for (int i = 0; i < N ; i++) {
			if ( fri[i].min <= f  && fri[i].max >= f ) {
				count++;
			}
			else {
			}
		}

		if ( count != 1 && count > ret ) {
			if ( count >= f ) {
				ret = f-1; 
			}
		}
	}
	cout << ret << endl;
	return 0;
}

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 && n-k<=minb){
      cout<<n-k-1<<endl;
      break;
    }else if(maxa<=n-k){
      num[indexb][0]=0;
      num[indexb][1]=100001;
    }else if(minb>=n-k){
      num[indexa][0]=0;
      num[indexa][1]=100001;
    }
  }
  if(k==n-1)
    cout<<"0"<<endl;
}