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 <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>

using namespace std;

int main(){

	int N,left,right,maximum;
	scanf("%d",&N);

	int* table = new int[100003];
	for(int i = 0; i <= 100002; i++){
		table[i] = 0;
	}

	for(int i = 0; i < N; i++){
		scanf("%d %d",&left,&right);
		table[left]++;
		table[right+1]--;
	}

	maximum = 0;
	for(int i = 1; i <= N+1; i++){
		table[i] += table[i-1];
		if(table[i]+1 >= i){
			maximum = i;
		}
	}

	printf("%d\n",min(N,max(maximum-1,0)));

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
int n,a[100005];

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

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

#define INF 1e9
#define llINF 1e18
#define MOD 1e9+7
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
using namespace std;
int main(){
  int n;cin>>n;
  vector<pair<int,int> >pii;
  int rui[100010]={};
  for(int i=0;i<n;i++){
    int a,b;cin>>a>>b;a-=2;b-=2;
    rui[a]++;
    rui[b+1]--;
  }
  int ma=0;
  int now=0;
  for(int i=0;i<n+1;i++){
    now+=rui[i];
    if(now>=i)
    ma=max(ma,now);
  }
  cout<<ma<<endl;
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define int long long
using ll=long long;
using vi=vector<int>;
using vl=vector<long long>;
using pii=pair<int,int>;
using pll=pair<long long,long long>;
#define ITR(i,c) for(auto i=begin(c);i!=end(c);++i)
#define FORE(x,c) for(auto &x:c)
#define REPF(i,a,n) for(int i=a,i##_len=(int)(n);i<i##_len;++i)
#define REP(i,n) REPF(i,0,n)
#define REPR(i,n) for(int i=(int)(n);i>=0;--i)
#define REPW(i,n) for(i=0;i<(int)(n);++i)
#define ALL(c) begin(c),end(c)
#define RALL(c) rbegin(c),rend(c)   // c++14
#define SZ(c) ((int)c.size())
#define EXIST(c,x) (c.find(x)!=end(c))
#define OUTOFRANGE(y,x,h,w) (y<0||x<0||y>=h||x>=w)
#define dump(...)
const int DX[9]={0,1,0,-1,1,1,-1,-1,0},DY[9]={-1,0,1,0,-1,1,1,-1,0};
#define INF (1001001001)
#define INFLL (1001001001001001001ll)
template<class T> ostream& operator << (ostream &os,const vector<T> &v) {
    ITR(i,v) os << *i << (i==end(v)-1 ? "" : " "); return os; }
template<class T> istream& operator >> (istream &is,vector<T> &v) {
    ITR(i,v) is >> * i; return is; }
template<class T> istream& operator >> (istream &is, pair<T,T> &p) {
        is >> p.first >> p.second; return is; }
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return 1;}return 0;}
//------------------------------------------------------------------------------
struct before_main_function {
    before_main_function() {
        #ifdef int
            #undef INF
            #define INF INFLL
            #define stoi stoll
        #endif
        cin.tie(0);ios::sync_with_stdio(false);
        cout<<setprecision(15)<<fixed;
    }
} before_main_function;
//------------------------------------------------------------------------------

inline void solve(int n) {
    vector<int> cnt(100010,0);
    REP(i,n) {
        int a,b;
        cin>>a>>b;
        cnt[a]++;
        cnt[b+1]--;
    }
    cnt[0]++;
    REP(i,100010) {
        cnt[i+1]+=cnt[i];
    }
    REPR(i,100010) {
        if(i<=cnt[i]) {
            cout<<i-1<<endl;
            return;
        }
    }
    cout<<0<<endl;
}
signed main() {
    int n;
    cin>>n;
    solve(n);
    return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main(){
  int N;
  cin >> N;
  int a,b;
  vector<int> c(1e6+2,0);
  vector<int> d(1e6+2,0);
  for(int i = 0; i< N ;i++){
    cin >> a >> b;
    if( a == b) d[a]++;
    else{
      c[a]++;
      c[b]--;
    }
  }
  for(int i = 0; i < c.size() ;i++){
    c[i+1] += c[i];
  }
  for(int i = 0;i<c.size();i++){
    c[i] += d[i];
  }
  for(int i =0; i<10;i++){
    cout << c[i];
  }
  int maxnum = 0;
  for(int i = 1; i < c.size() ; i++){
    if(i <= c[i]+1) maxnum = i-1;
  }
  cout << maxnum << endl;
  return 0;
}

C++: #include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
#define int long long

const int MAX = 100001;

int n;
int a[MAX], b[MAX];

signed main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
  }
  int imos[MAX + 2];
  fill(imos, imos + n + 2, 0);
  for (int i = 0; i < n; i++) {
    imos[a[i]]++;
    imos[b[i] + 1]--;
  }
  for (int i = 1; i < n + 2; i++) {
    imos[i] += imos[i - 1];
  }
  for (int i = n + 1; i >= 1; i--) {
    if (imos[i] >= i) {
      cout << i - 1 << endl;
      return 0;
    }
  }
  cout << 0 << endl;
  return 0;
}

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

int data[100001][100002] = {0};

int main(){
	int n;
	cin >> n;

	//init
	/*
	for(int i = 0; i < 100001; i++){
		for(int j = 0; j < 100002; j++) data[i][j] = 0;
	}*/

	//input
	for(int i = 1; i <= n; i++){
		int r, l;
		cin >> r >> l;
		for(int j = r; j <= l; j++) data[i][j] = 1;
	}

	vector<int> v;
	for(int i = 1; i <= n; i++){
		int ok = 0;
		for(int j = 1; j <= n; j++){
			if(data[j][i]) ok++;
		}
		if(ok + 1 >= i) v.push_back(i);
	}

	/*
	for(int i = 1; i <= n; i++){
		for(int j = 1; j <= n; j++) cout << data[i][j] << " ";
		cout << endl;
	}
	for(int i = 0; i < (int)v.size(); i++) cout << v[i] << endl;
	*/

	if(v.size()){
		sort(v.begin(), v.end());
		cout << v[v.size() - 1] - 1 << endl;
	}
	else cout << 0 << endl;

	return 0;
}

C++: #include <iostream>
#include <vector>
using namespace std;
int main(){
  int N;
  cin >> N;
  int a,b;
  vector<int> c(1e6+2,0);
  vector<int> d(1e6+2,0);
  for(int i = 0; i< N ;i++){
    cin >> a >> b;
    if( a == b) d[a]++;
    else{
      c[a]++;
      c[b]--;
    }
  }
for(int i = 0;i<c.size();i++){
  c[i] += d[i];
}
  for(int i = 0; i < c.size() ;i++){
    c[i+1] += c[i];
  }
  int maxnum = 0;
  for(int i = 1; i < c.size() ; i++){
    if(i == c[i]+1) maxnum = i-1;
  }
  cout << maxnum << endl;
  return 0;
}