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) == true) {
            left = middle + 1;
        } else {
            right = middle;
        }
    }
    cout << left - 2 << endl;

    return 0;
}

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

using ll=long long;
#define int ll

#define FOR(i,a,b) for(int i=int(a);i<int(b);i++)
#define REP(i,b) FOR(i,0,b)

int read(){
	int i;
	scanf("%lld",&i);
	return i;
}

signed main(){
	int i;
	int N=read();
	int ans=0;
	int a[100010]={0},b[100010]={0};
	int sum[N+3]={0};
	
	sum[0] = 1; //include me
	
	REP(i,N){
		a[read()] ++;
		b[read()+1] --;
	}
	
	for(i=1;i<=N+1;i++){
		sum[i] += sum[i-1] + a[i] + b[i];
		//cout << sum[i] << endl;
	}
	
	for(i=1;i<=N+1;i++){
		if(sum[i]>=i){
			ans = i-1;
		}
	}
	cout << ans << endl;
}

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

int N;
int imosu[IMOSU_MAX];
int a,b;
int ans;

int main() {
	cin>>N;
	for (int i = 0; i < N; i++) {
		cin>>a>>b;
		imosu[a]++;
		imosu[b+1]--
		;
	}
	imosu[0]++;
	for (int i = 1; i < IMOSU_MAX; i++) {
		imosu[i]+=imosu[i-1];
		if(imosu[i]>ans && imosu[i]==i)ans=imosu[i];
	}
	cout<<ans-1<<endl;
	//cin>>N;
	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 ???????????????????????´?????????????????????
19:49:41 WA
19:50:47 ??¨?????????????????£????????????Case #10????????????
19:52:03 ?????°????°?????????´????????????????????¨???????????????????????????????????????????????????
19:54:34 possible???????????§???????????¨???????¨?????????????????????????????????????
19:56:34 ????????¨??¢?´¢??§?????????????????§??????
19:59:24 ??¨??¢?´¢
*/
#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 i = 100000;
	while(i>0){
		if(isPossible(i)){
			cout << i << endl;
			return 0;
		}
		i--;
	}
	cout << 0 << endl;
	
}

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

#define ll long long int 

using namespace std;
void solve();

char del=' ';

int main() {

solve();

}

void solve(){
   ll ans=0;

   ll n;
   cin >> n;

   vector<ll> a(100002);  //1-indexedで考える
   for (vector<ll>::iterator itr = a.begin(); itr < a.end(); itr++) {
      *itr=0;
   }
   vector<ll> _min(n);
   vector<ll> _max(n);

   for (ll i = 0; i < n; i++) {

      cin >> _min[i] >> _max[i];
      _min[i]--;_max[i]--;

   }
   
   for (ll i = 0; i < n; i++) {
      
      //imosの準備
         a[_min[i]]+=1;
         a[_max[i]]-=1;

   }

   for (vector<ll>::iterator itr = a.begin()+1; itr < a.end(); itr++) {
      *itr+=*(itr-1);
   }

   for (ll i=1;i<n+1;i++) {
      if(a[i]>=i)ans=max(ans,a[i]);
   }

   cout << ans << endl;
}

C++:  #include <iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
#define P pair<int,int>
#define mp make_pair


int t[100002];
int x[100002];
int y[100002];
int z[100002];
int n;
int main(){
	cin>>n;
	//for(int i=0;i<=n;i++)t[i]=1;
	for(int i=0;i<n;i++){
		int a,b;
		cin>>a>>b;
			for(int j=a-1;j<b;j++)t[j]++;
			y[a]++;
			z[b]--;
	}
	
	int ans=0;
	//for(int i=0;i<=n;i++)cout<<t[i]<<" ";cout<<endl;
	for(int i=0;i<=n+1;i++)if(t[i]>=i)ans=i;//t[i]??\??????????????°i???????????°
	//if(ans!=0)ans--;
	cout<<ans<<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[100001];
  lp(i,100001){
    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];
  }
  cout<<ans-1<<endl;
  return 0;
    
}

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

int main(){
    int N; cin >> N;
    int a[100010],imos[100010];
    fill(a,a+100010,0);
    fill(imos,imos+100010,0);
    for(int i = 0; i < N; i++){
        int s,t; cin >> s >> t;
        s--; t--;
        a[s]++;
        a[t+1]--;
    }
    for(int i = 1; i <= 100005; i++){
        imos[i] = imos[i-1]+a[i];
    }
    int ans = 0;
    for(int i = 0; i <= 100005; i++){
        if (imos[i] >= i){
            ans = i;
        }
    }
    cout << ans << endl;
}