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<string.h>
using namespace std;

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

	int left[100002],right[100002];
	memset(left,0,sizeof(left));
	memset(right,0,sizeof(right));

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

	int before=left[2];
	int ans=0;
	if(left[2]>=1) ans=1;

	for(int num=3;num<=N+1;num++){
		int now=before+left[num]-right[num-1];
		if(now>=num) ans=max(ans,num-1);
		before=now;
	}
	cout<<ans<<endl;
}

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

using namespace std;

int main()
{
	array<int, 100000> friends = {1, 0,};
	int n = 0;
	cin >> n;

	int max_index = 0;

	for(auto i = 0; i < n; ++i)
	{
		int under;
		int over;
		cin >> under >> over;

		friends[under - 1]++;
		friends[over]--;
	}

	int ans = 0;
	for(int i = 1; i <= n; ++i)
	{
		friends[i] = friends[i] + friends[i - 1];
		if(friends[i] > ans && friends[i] - i >= 0){
			ans = max(ans, friends[i]);
		}
	}
	ans = max(ans - 2, 0);
	cout << ans << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
 
#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)
 
#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=(int)(a)-1;i>=(int)(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)
 
#define _all(arg) begin(arg),end(arg)
#define uniq(arg) sort(_all(arg)),(arg).erase(unique(_all(arg)),end(arg))
#define getidx(ary,key) lower_bound(_all(ary),key)-begin(ary)
#define clr(a,b) memset((a),(b),sizeof(a))
#define bit(n) (1LL<<(n))
 
// #define DEBUG
 
#ifdef DEBUG
    #define dump(...) fprintf(stderr, __VA_ARGS__)
#else
    #define dump(...)
#endif
 
template<class T>bool chmax(T &a, const T &b) { return (a<b)?(a=b,1):0;}
template<class T>bool chmin(T &a, const T &b) { return (b<a)?(a=b,1):0;}
 
using namespace std;
using ll=long long;
using vi=vector<int>;
using vll=vector<ll>;
 
const double EPS = 1e-10;
const double PI = acos(-1.0);
const ll inf =1LL << 62;
const ll mod=1000000007LL;
const int dx[4]={1,0,-1,0};
const int dy[4]={0,1,0,-1};
 
 
ll extgcd(ll a,ll b,ll& x,ll& y){x=1,y=0;ll g=a;if(b!=0) g=extgcd(b,a%b,y,x),y-=a/b*x;return g;}
ll ADD(const ll &a, const ll &b,const ll &mod) { return (a+b)%mod;}
ll SUB(const ll &a, const ll &b,const ll &mod) { return (a-b+mod)%mod;}
ll MUL(const ll &a, const ll &b,const ll &mod) { return (1LL*a*b)%mod;}
ll DIV(const ll &a, const ll &b,const ll &mod) {ll x,y; extgcd(b,mod,x,y);return MUL(a,(x+mod)%mod,mod);}
 
random_device rd;
mt19937 mt(rd());
uniform_int_distribution<int> dice(1,6);
uniform_real_distribution<double> score(0.0,10.0);

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

    vi sum(n + 2);
    rep(loop, n){
        int a, b; cin >> a >> b;
        chmin(b, n);
        sum[a]++, sum[b + 1]--;
    }

    int res = 0;
    rep(i, 1, n + 1){
        sum[i] += sum[i - 1];

        if(sum[i] + 1 >= i){
            chmax(res, i - 1);
        }
    }

    cout << res << endl;

    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>
#include<queue>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> P;
typedef long long int ll;
typedef pair<ll,ll> LLP;
int dx[4] = {1,0,0,-1};
int dy[4] = {0,1,-1,0};
#define INF 1000000007
#define MOD 1000000007
#define ARRAY_MAX 100005

int imos[ARRAY_MAX];

int main(){

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

    for(int i = 1;i < ARRAY_MAX;i++){
        if(i <= imos[i]+1){
            ans = i-1;
        }
    }
    cout << ans << endl;

    return 0;
}

C++: #include <iostream>
using namespace std;
int n,i,a[100002],b,c,m,j;
int main(){
  for(cin>>n;i<n;i++){
    cin>>b>>c;
    for(j=b;j<=c;j++)a[j]++;
  }
  for(i=0;i<=n;i++)m=max(m,a[i]);
  cout<<m<<endl;
}

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];
    }
    int left = 1;
    int right = n + 1;

    while (left < right) {
        int middle = (left + right) / 2; // left??????
        if (check(table, middle) == true) {
            left = middle + 1;
        } else {
            right = middle;
        }
    }
    cout << left - 2 << endl; // left???1?????§?????? + ???????????????
/*
    while (left < right) {
        int middle = (left + right) / 2 + 1; // right??????
        if (check(table, middle) == true) {
            left = middle;
        } else {
            right = middle - 1;
        }
    }
    cout << left - 1 << endl; // ???????????????
*/

    return 0;
}

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;
}

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

using namespace std;

int main ()
{
	int N;
	cin >> N;
	vector<int>data ( 100005 );
	data[0]++;
	data[N + 4]--;
	for( size_t i = 0; i < N; i++ )
	{
		int a , b;
		cin >> a >> b;
		data[a]++;
		data[b + 1]--;
	}
	for( size_t i = 1; i < 100005; i++ )
	{
		data[i] += data[i - 1];
		//cout << data[i] << endl;
	}
	for( int i = N+1; i >= 0; i-- )
	{
		if( data[i] >= i )
		{
			cout << i -1<< endl;
			return 0;
		}
	}
}