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 <vector>
#include <algorithm>
#include <cmath>
using namespace std;


int main() {
    int N;
    cin >> N;
    vector<int> v(100003, 0);
    for(int i=0; i<N; ++i) {
        int a, b;
        cin >> a >> b;
        ++v[a-1];
        --v[b];
    }
    int ret = 0;
    for(int i=1; i<v.size(); ++i) {
        v[i] += v[i-1];
        if(v[i] >= i) {
            ret = i;
        }
    }
    cout << ret << endl;
}

C++: #include <iostream>
#include <queue>
#include <map>
#include <cmath>
#include <algorithm>
#include <queue>
#include <cstdarg>
#include <set>
#include <limits>
#include <iomanip>

using namespace std;
#define INF 2e9 + 9
#define MOD 1000000007
#define ALL(v) v.begin() , v.end()
#define Int int64_t
#define pll pair<Int,Int>
template<class T> using pql = priority_queue<T, vector<T>>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
#define ll(n) Int n; cin >> n;
#define LL(...) Int __VA_ARGS___; cin >> __VA_ARGS___;
#define OUT(...) cout << __VA_ARGS___;
#define RP(n, i, s) for(Int i = s ;i < n ;i++)
#define REP(n, i) for (Int i = 0; i < n; i++)
#define REPe(n, i) for (Int i = 1; i <= n; i++)
#define RPe(n, i, s) for (Int i = s; i <= n; i++)
#define upd_max(maxer, updater) if (maxer < updater) maxer = updater;

void ansYes(bool flag) {
    if (flag) cout << "Yes" << endl; else cout << "No" << endl;
}

void ansYES(bool flag) {
    if (flag) cout << "YES" << endl; else cout << "NO" << endl;
}

struct node {
    Int to;
    Int length;
};


vector<Int> divisor(Int n) {
    vector<Int> ret;
    for (Int i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            ret.push_back(i);
            if (i * i != n) ret.push_back(n / i);
        }
    }
    return (ret);
}

/*
vector<vector<int>> to;
vector<int> flag;
vector<int> c;


Int dfs(int from, int mae) {
    flag[from] = 1;
    Int mini = 0;
    for (auto next : to[from]) {
        if (next == mae) continue;
        if (flag[next] == 1) {
            return -1;
        } else {
            mini = min(mini, dfs(next, from));
        }
    }
    return mini;
}*/

int dx8[8] = {0, -1, -1, -1, 0, 1, 1, 1};
int dy8[8] = {-1, -1, 0, 1, 1, 1, 0, -1};

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

struct datas {
    Int max;
    Int size;
};

class ContainChecker {
protected:
    Int minx, miny, maxx, maxy;
public:
    ContainChecker(Int minx, Int miny, Int maxx, Int maxy) : minx(minx), miny(miny), maxx(maxx), maxy(maxy) {};

    bool in(Int x, Int y) {
        return (minx <= x && miny <= y && x <= maxx && y <= maxy);
    }
};


int main(void) {
    Int n;
    cin >> n;
    vector<Int> memo(100003);
    REP(n,i){
        Int a,b;
        cin >> a >> b;
        a--; b--;
        memo.at(a)++;
        memo.at(b+1)--;
    }
    Int maxer = 0;
    REP(100002,i){
        memo.at(i+1) += memo.at(i);
        if (i <= memo.at(i)) {
            maxer = max(maxer, i);
        }else{
            //maxer = max(maxer, memo.at(i));
        }
    }
    cout << maxer << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;

int main(){
	int n;
	cin>>n;
	ll a[n],b[n];
	for(int i=0;i<n;i++)cin>>a[i]>>b[i];
	ll anst[100005];
	memset(anst,0,sizeof(anst));
	for(int i=0;i<n;i++){
		anst[a[i]-2]+=1;
		anst[b[i]-1]-=1;
	}
	ll ans=0;
	for(int i=1;i<n;i++){
		anst[i]+=anst[i-1];
	}

	for(int i=0;i<n;i++){
		if(anst[i]>=i+1){
//			ans = max(anst[i],ans);
			ans = i+1;
			//if(i+1>=n)break;
			//if(anst[i+1]<anst[i])ans-=(anst[i]-anst[i+1]);
		}
		//if(anst[ans]>ans)i=ans;
	}
	cout<<ans<<endl;
}

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

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> v(100000 + 2 + 1, 0);
    for (int i = 0; i < n; ++i) {
        int a;
        int b;
        cin >> a >> b;
        v[b + 1] -= 1;
        v[a] += 1;
    }

    for (int i = 1; i < v.size() - 1; ++i) {
        v[i] = v[i - 1] + v[i];
    }
    int max_{0};
    for (int i = 0; i < v.size(); ++i) {
        if (v[i] != 0 && (i - 1) <= v[i]) {
            max_ = i;
        }
    }
    if (max_ != 0) {
        cout << (max_ - 1) << endl;
    }
    else {
        cout << max_ << endl;
    }
}

C++: // #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
 
int main() {
    int N; scanf("%d", &N);
    const int MAXN = 100010;
    vector<int> seg(MAXN+5);
    seg[0] = 1;
    for(int i=0; i<N; i++) {
        int a, b;
        scanf("%d%d", &a, &b);
        seg[a]++, seg[b+1]--;
    }

    for(int i=1; i<=MAXN+3; i++) seg[i] += seg[i-1];

    int ans = -1;
    for(int i=1; i<=MAXN; i++) {
        if(seg[i] >= i) {
            chmax(ans, i-1);
        }
    }
    printf("%d\n", ans);
    return 0;
}

C++: // 2017-02-03
#include <iostream>
using namespace std;

int main() {
	int n;
	int a[100001 + 1] = {};
	int b[100001 + 1] = {};
	cin >> n;
	for (int i = 0; i < n; i++) {
		int x, y;
		cin >> x >> y;
		a[x]++; b[y]++;
	}
	int p = 1;
	int r = 1;
	for (int i = 2; i <= 100001; i++) {
		p = p + a[i] - b[i - 1];
		if (i <= p) r = i;
	}
	cout << r - 1 << endl;
	return 0;
}