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>
#define REP(i, n) for(int i = 0;i < n;i++)
using namespace std;
int main(){
    int n;
    int cnt[100100];
    memset(cnt,0,sizeof(cnt));
    cin>>n;
    REP(i,n){
        int a,b;
        cin>>a>>b;
        cnt[a]++;cnt[b+1]--;
    }
    int ans=0;
    REP(i,100000)cnt[i+1]+=cnt[i];
    REP(i,n+1){
        if(cnt[i]+1>=i){
            ans=i-1;
        }
    }
    cout<<ans<<endl;
}

C++: #include "bits/stdc++.h"

#define REP(i,n) for(ll i=0;i<n;++i)
#define RREP(i,n) for(ll i=n-1;i>=0;--i)
#define FOR(i,m,n) for(ll i=m;i<n;++i)
#define RFOR(i,m,n) for(ll i=n-1;i>=m;--i)
#define ALL(v) (v).begin(),(v).end()
#define PB(a) push_back(a)
#define UNIQUE(v) v.erase(unique(ALL(v)),v.end());
#define DUMP(v) REP(aa, (v).size()) { cout << v[a]; if (a != v.size() - 1)cout << " "; else cout << endl; }
#define INF 1000000001ll
#define MOD 1000000007ll
#define EPS 1e-9

const int dx[8] = { 1,1,0,-1,-1,-1,0,1 };
const int dy[8] = { 0,1,1,1,0,-1,-1,-1 };


using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vi> vvi;
typedef vector<vl> vvl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
ll max(ll a, int b) { return max(a, ll(b)); }
ll max(int a, ll b) { return max(ll(a), b); }
ll min(ll a, int b) { return min(a, ll(b)); }
ll min(int a, ll b) { return min(ll(a), b); }
///(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)(?´????????`)///
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	vi v(100002);
	int n;
	cin >> n;
	REP(i, n) {
		int a, b;
		cin >> a >> b;
		v[b-1]--;
		v[a - 2]++;
	}
	int ans = 0;
	REP(i, v.size() - 1)v[i + 1] += v[i];
	REP(i, v.size()) {
		if (i + 1 <= v[i])ans = i + 1;
	}
	cout << ans << endl;
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<map>
using namespace std;
vector<pair<int,int> > F;
int solve(int low, int high, int ans);
int main(){
  int n;
  cin >>n;
  for(int i=0,a,b; i<n; i++){
    cin >>a>>b;
    F.push_back(make_pair(a,b));
  }
  cout <<solve(0,n,0)<<endl;
  return 0;
}
int solve(int low, int high, int ans){
  int mid=(low+high)/2,num=0;
  if(high<=low || mid == ans) return ans;
  for(int i=0; i<F.size(); i++)
    if(mid+1 >= F[i].first && mid+1 <= F[i].second)
      num++;
  if(mid <= num) return solve(mid,high,mid);
  return solve(low,mid,ans);
}

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

int d[100010];

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

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

	int ans = 0, s = 1;
	for(int i = 1; i <= N; i++) {
		s += d[i];
		if(s >= i) {
			ans = i - 1;
		}
	}
	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] --;
  }

  for(int i=0; i<MAX; i++) {
    dp[i+1] += dp[i];
  }
  
  int mx = 0;
  for(int i=0; i<MAX; i++) {
    if(dp[i]+1>=i) mx = max(mx, dp[i]);
  }
  cout << mx << endl;
  
  return 0;
}

C++: #include <iostream>
#include <complex>
#include <vector>
#include <unordered_map> 
#include <algorithm>

/*
 * http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2331&lang=jp
 * test of c++
 */


template <typename T>
std::vector<std::vector<T>> read_input() {
    std::string input_str;

    std::getline(std::cin, input_str);
    int arr_size = std::stoi(input_str);
    std::vector<std::vector<T>> input_vec(arr_size, std::vector<int>(2, 0));

    for (size_t i = 0; i < arr_size; i++) {
        std::getline(std::cin, input_str);
        std::istringstream iss(input_str);
        T begin, end;
        iss >> begin >> end;

        // this is for myself
        begin -= 1;
        end -= 1;
        input_vec.at(i).at(0) = begin;
        input_vec.at(i).at(1) = end;
    }
    return input_vec;
}


bool search(std::vector<std::vector<int>> input_vec, int ans) {
    if (ans == 0) {
        return true;
    }
    int count = 0;
    for (int i = 0; i < input_vec.size(); i++) {
        std::vector<int> person = input_vec.at(i);
        if (person.at(0) <= ans && ans <= person.at(1)) {
            count++;
        }
        if (count == ans) {
            return true;
        }
    }
    return false;
}


int main(int argc, char const *argv[]) {
    std::vector<std::vector<int>> input_vec = read_input<int>();
    int ans;

    // num: #friend
    for (int num = 0; num < input_vec.size(); num++) {
        if (search(input_vec, num)) {
            ans = num;
        } else {
            break;
        }
    }
    std::cout << ans << std::endl;
    return 0;
}

C++: #include <set>
#include <cstdio>
#define REP(i,n) for(int i=0; i<(int)(n); i++)

inline int getInt(){ int s; scanf("%d", &s); return s; }

using namespace std;

int a[100000 + 1];
int b[100000 + 1];

int main(){
  int n = getInt();

  REP(i,n){
    int aa = getInt();
    int bb = getInt();

    a[aa]++; b[bb]++;
  }

  int ans = 0;
  int peo = 1;

  REP(i,100000 + 1){
    peo += a[i];
    if(i > 0) peo -= b[i];

    if(peo >= i){
      ans = i - 1;
    }
  }

  printf("%d\n", ans);

  return 0;
}

C++: #include<iostream>
using namespace std;
#define REP(i,b,n) for(int i=b;i<n;i++)
#define rep(i,n)   REP(i,0,n)

const int N = 100010;
int in[N];
int de[N];

int main(){
  int n;
  while(cin>>n){
    rep(i,N)in[i] = 0,de[i] = 0;
    rep(i,n){
      int a,b;
      cin>>a>>b;
      in[a]++;
      de[b+1]++;
    }
    //xテつ人テつづつセテつづδkテつづδ暗つ人テつづつェテつ嘉つステつ人テつづつ「テつづδゥテつづつゥテつづδーテつ静つ氾つづつヲテつづδゥ
    int ans = 0;
    int now = 0;
    rep(i,N){
      now += in[i];
      now -= de[i];
      if (now+1 >= i)ans = i;
      /*
      //if (now >= i)ans = i;
	2
	3 3
	3 3
	テつづδつ療つ偲つづつソテつづδゥ
       */
    }
    if (ans != 0)ans--;
    cout << ans << endl;
  }
}

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

int main() {

    int n, imos[100003] = {0};

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

    for(int i = 1; i < 100003; i++) {
        imos[i] += imos[i - 1];
    }

    int maxi = 0;
    for(int i = 0; i <= n; i++) {
        if(imos[i] >= i) maxi = max(maxi, i);
    }
    cout << maxi - 1 << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int N,a,b;
  int max=0;
  int ans=1;
  int man[100001];
  for(int i=0;i<100001;i++){
    man[i]=0;
  }
  cin>>N;
  for(int i=0;i<N;i++){
    cin>>a>>b;
    for(int j=a;j<=b;j++) man[j]++;
  }
  for(int i=2;i<=N+1;i++){
    if(man[i]>max && man[i]>=i-1){
      max=man[i];
      ans=i;
    }
  }
  cout<<ans-1<<endl;
  return 0;
}