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;

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 N=read();
	int a[N],b[N];
	int check[100050]={0};
	REP(i,N){
		a[i]=read();
		b[i]=read();
	}
	
	REP(i,N){
		check[a[i]] += 1;
		check[b[i]] -=1;
	}
	
	int sum=1;
	int ans=0;
	REP(i,N+1){
		sum += check[i];
		if(sum>=i) ans=i;
	}
	cout << ans-1 << endl;
}

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 ????????¢?´¢????????????
*/
#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;
	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 = 1;
	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;

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

    int n, a, b;

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

    for (int i = 1; i <= n; ++i)
        sum[i] += sum[i - 1];

    int ans = 0;
    for (int i = 0; i < n; ++i)
        if (i <= sum[i])
            ans = max(ans, min(i, sum[i]));

    cout << ans << endl;

    return 0;
}

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

int main(){
  scanf("%d",&n);
  for(int i=0;i<n;i++){
    scanf("%d %d",&a,&b);
    t[a-1]++;
    t[b]--;
  }
  int ans=0;
  for(int i=1;i<=100000;i++){
    t[i]+=t[i-1];
    if(i<=t[i])ans=i;
  }
  cout<<ans<<endl;
  return 0;
}

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

vector<pair<int, int>> ab(100002);
vector<int> k(100002);

int main() {
	int N;
	cin >> N;
	for (int i = 0; i < 100002; i++) {
		ab[i].first = 0; 
		ab[i].second = 0;
}

	for (int i = 1; i <= N; i++) {
		cin >> ab[i].first >> ab[i].second;
		k[ab[i].first]++;
		k[ab[i].second]--;
	}

	int sum = 0;
	int ans = 0;

	for (int i = 1; i < 100002; i++) {
		sum += k[i];
		if (sum + 1 >= i) {
			ans = i;
		}
	}
	cout << ans << endl;
	return 0;
}

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) {
		if(imos[i]+1>=i) ans=max(ans,i);
	}
	cout<<max(0,ans-1)<<endl;

	return 0;
}

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

int main(int argc, char const *argv[])
{
	int n;
	cin >> n;
	int imos[100005]={}, a, b;
	for (int i = 0; i < n; ++i)
	{
		cin >> a >> b;
		++imos[a];
		--imos[b+1];
	}
	int cs[100005]={};
	cs[0]=imos[0];
	for (int i = 1; i < 100005; ++i)
	{
		cs[i] = cs[i-1] + imos[i];
	}

	int ans=0;
	for (int i = 2; i < 100005; ++i)
	{
		if(cs[i]+1>=i) ans = i-1;
	}
	cout << ans << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

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

using namespace std;
int main(void)
{
  int n;
  cin >> n;
  vector<int> f(131072,0);
  REP(i,n){
    int l,r;
    cin >> l >> r;
    ++f[l];
    --f[r+1];
  }
  ++f[0];
  REP(i,131000) f[i+1]+=f[i];
  int answer=0;
  REP(i,131000) if(f[i]>=i) answer=i-1;
  cout << answer << endl;
  return 0;
}

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

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> v(100000 + 2, 0);
    for (int i = 0; i < n; ++i) {
        int a;
        int b;
        cin >> a >> b;
        for (int j = a; j <= b; ++j) {
            v[j] += 1;
        }
    }
    int max_{0};
    for (int i = 0; i < v.size(); ++i) {
        if (v[i] != 0 && i <= v[i]) {
            max_ = i - 1;
        }
    }
    cout << max_ << endl;
}

C++: #include <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
   
using namespace std;

int main()
{
    int n;
    cin >> n;
    int result[100003] = {};
    for(int i = 0; i < n; i++){
        int l, r;
        cin >> l >> r;
        result[l - 1]++;
        result[r]--;
    }
    int ans = 0;
    for(int i = 1; i < n + 1; i++){
        result[i] += result[i - 1];
        // cout << result[i] << endl;
        if(result[i] >= i) ans = max(i, ans);
    }
    cout << ans << endl;
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<functional>
#include<queue>
using namespace std;
typedef long long ll;
int main()
{
	int n;
	cin >> n;
	int sum[100010] = {};
	for (int i = 0; i < n; i++) {
		int a, b;
		cin >> a >> b;
		sum[a - 1]++;
		sum[b]--;
	}
	int ans = 0;
	for (int i = 1; i < n; i++) {
		sum[i] += sum[i - 1];
	}
	for (int i = 0; i <= n; i++) {
		if (sum[i] >= i) {
			ans = i;
		}
	}
	cout << ans << endl;	return	0;
}