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 <bits/stdc++.h>
using namespace std;

#define INF 1.1e9
#define LINF 1.1e18
#define FOR(i,a,b) for (int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()
#define pb push_back
#define pf push_front
#define fi first
#define se second
#define BIT(x,n) bitset<n>(x)
#define PI 3.14159265358979323846

typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> PP;

//-----------------------------------------------------------------------------

int n,imos[100010];

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

	cin>>n;
	REP(i,n) {
		int a,b;cin>>a>>b;
		imos[a]++,imos[b+1]--;
	}
	REP(i,100000) imos[i+1]+=imos[i];
	int ans=0;
	REP(i,n+2) {
		ans=max(ans,imos[i]);
	}
	cout<<ans-1<<endl;

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=1;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int main(){
  int n;
  cin>>n;
  int a[100002];
  lp(i,100002){
    a[i]=1;
  }
  lp(i,n){
    int x,y;
    cin>>x>>y;
    for(int i=x;i<=y;i++){
      a[i]+=1;
    }
  }
  int ans=0;
  lp(i,n+1){
    if(ans<a[i]&&a[i]>=i) ans=i;
  }
  cout<<ans<<endl;
  return 0;
    
}

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

int dp[100002];

int main() {

    int n;

    cin >> n;

    int a,b;

    for(int i=0;i<n;i++){
        cin >> a >> b;
        a--;b--;
        for(int j=a;j<=b;j++){
            dp[j]++;
        }
    }

    int ans = 0;

    for(int i=2;i<=100001;i++){
        if(dp[i]>=i) {
            ans = max(ans, i);
        }
    }

    cout << ans << endl;

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max() / 32 * 15 + 208;

int main() {
    int n;
    cin >> n;
    const int SIZE = 100010;
    array<int, SIZE> v{};
    for (int i = 0; i < n; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        v[a-1]++; v[b]--;
    }
    for (int i = 1; i < SIZE; ++i) {
        v[i] += v[i-1];
    }
    int ans = 0;
    for (int i = 0; i < SIZE; ++i) {
        if(v[i] >= i) ans = i;
    }
    cout << ans << "\n";
    return 0;
}

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; i >= 0; --i) {
    itr1 = upper_bound(A.begin(),A.end(),i+1);
    itr2 = upper_bound(B.begin(),B.end(),i+1);
    s = distance(A.begin(), itr1);
    t = distance(B.begin(), itr2);
    if (s-t == i) {
      ans = s-t;
      break;
    }
  }
  cout << ans << endl;
}

C++: #include<iostream>
using namespace std;
int main(){
  int ma[100005]={};
  int n;
  cin >> n;
  int i,j,k;
  int a,b;
  for(i=0;i<n;i++){
    cin >> a >> b;
    for(j=a;j<=b;j++) ma[j]++;
  }
  int m=0;
  for(i=0;i<100001;i++){
    //if(i<10)
    //cout << i <<":" << ma[i]<<endl;
    if(i<=ma[i]+1)
      m=max(m,i-1);
  }
  cout << m << endl;
  return 0;
}

C++: /*
URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?lang=jp&id=2331
?????????: A Way to Invite Friends
?????°:
19:34:14 ??????????????????
19:38:21 ??\????????????????????????????§£?????????
19:39:29 ?????§???????±???????????????????????????¢?´¢??§??????????????????
19:46:53 ????????¢?´¢????????????
19:47:16 WA
19:48:34 ???????????????????????´?????????????????????
*/
#include <iostream>

using namespace std;

int N;
int a[114514];
int b[114514];

// is it possible to hang out WITH n people?
bool isPossible(int n)
{
	if(n > 100000) return false;
	if(n <= 0) return true;
	int count=0;
	for(int i=0; i<N; i++){
		if((a[i] <= n+1) && (n+1 <= b[i])){
			count++;
		}
	}
	return(count >= n); // true if n people exists
}

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

	int l = 0;
	int u = 100001;
	// l <= ans < u; isPossible(l) && !isPossible(u)
	while(true){
		if(l+1 == u){break;}
		int mid = (l+u)/2;
		if(isPossible(mid)){
			l = mid;
		} else {
			u = mid;
		}
	}
	cout << l << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=1;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int main(){
  int n;
  cin>>n;
  int a[100002];
  lp(i,100002){
    a[i]=1;
  }
  lp(i,n){
    int x,y;
    cin>>x>>y;
    for(int i=x;i<=y;i++){
      a[i]+=1;
    }
  }
  int ans=0;
  lp(i,n+1){
    if(ans<a[i]&&a[i]>=i) ans=a[i];
    else if(ans<i && a[i]>=i) ans=i;
  }
  cout<<ans-1<<endl;
  return 0;
    
}

C++: #include<bits/stdc++.h>
#define range(i,a,b) for(int i = (a); i < (b); i++)
#define rep(i,b) range(i,0,b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int main(){
    int n;
    while(cin >> n){
        int count[100001] = {0}, ans = 0, sum = 0;
        rep(i,n){
            int a, b;
            cin >> a >> b;
            a--;
            count[a]+= 1;
            count[b]-= 1;
        }
        rep(i,n + 1){
            sum+=count[i];
            if(sum >= i) ans = i;
        }
        cout << ans << endl;
    }
}

C++: #include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
  int i,j;
  int n;
  cin>>n;
  int a[100000],b[100000];
  for(i=0;i<n;++i)
    cin>>a[i]>>b[i];
  int mn=1,mx=n+2;
  for(i=0;i<18;++i){
    int ct=1;
    for(j=0;j<n;++j){
      if(a[j]<=(mn+mx)/2&&(mn+mx)/2<=b[j])
	++ct;
    }
    if(ct<(mn+mx)/2)
      mx=(mn+mx)/2;
    else
      mn=(mn+mx)/2;
  }
  cout<<mn-1<<endl;
  return 0;
}

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=2; i<MAX; i++) {
    dp[i+1] += dp[i];
  }
  
  int mx = 0;
  for(int i=2; i<MAX; i++) {
    if(dp[i]+1>=i) mx = max(mx, dp[i-1]);
  }
  cout << mx << endl;
   
  return 0;
}

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

using namespace std;
using i64 = long long int;
using ui64 = unsigned long long int;

//////
using pii = pair<int,int>;

bool isok(vector<pii> &ab, int n){
  int acc = 0;
  for(auto&& x:ab){
    if(x.first <= n && n <= x.second)acc++;
  }
  return acc>=n;
}

int main(){
  int n;
  cin >> n;
  vector<pii> ab(n);
  for(auto&& x:ab)cin >> x.first >> x.second;
  int res = 0;bool f = false;
  for(int i=1;i<=n;i++){
    if(isok(ab,i+1)){
      f = true;
      res = i;
    }
  }
  cout << res << endl;
}