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>

using namespace std;

#define MAX_N

struct data_t{
    int max;
    int min;
};

struct data_a{
    int max;
    int min;
};

bool asc( const data_t& left, const data_t& right ) {
    return left.max == right.max ? left.min < right.min : left.max < right.max;
};

bool asc_a( const data_t& left, const data_t& right ) {
    return left.min == right.min ? left.max > right.max : left.min > right.min;
};

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

    vector<data_t> data_array(n);

    for(int i=0;i<n;i++){
        cin >> data_array[i].min >> data_array[i].max;
        cin.ignore();
    }

    sort(data_array.begin(),data_array.end(),asc);

/*
    cout << "-----------------" << endl;
    for(int i=0;i<n;i++){
        cout << data_array[i].min << "," << data_array[i].max << endl;
   }
*/
    int ans = n+1;
    for(int i=0;i<ans-1;i++){
        if(data_array[i].max < ans){
            ans--;
            for(int j=i;j<ans-1;j++){
                data_array[j]=data_array[j+1];
            }
            i--;
        }else{
            break;
        }
    }

    /*
    cout << "-----------------" << endl;
    for(int i=0;i<ans-1;i++){
        cout << data_array[i].min << "," << data_array[i].max << endl;
    }*/

    sort(data_array.begin(),data_array.begin()+ans-1,asc_a);
/*
    cout << "KOJUN-----------------" << endl;
    for(int i=0;i<ans-1;i++){
        cout << data_array[i].min << "," << data_array[i].max << endl;
    }
*/
    for(int i=0;i<ans-1;i++){
        if(data_array[i].min > ans){
             ans--;
            for(int j=i;j<ans-1;j++){
                data_array[j]=data_array[j+1];
            }
           
            i--;
        }else{
            break;

        }
    }
/*
    cout << "-----------------" << endl;
    for(int i=0;i<ans-1;i++){
        cout << data_array[i].min << "," << data_array[i].max << endl;
    }
*/
    cout << ans-1 << endl;

    return 0;

}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;

const int N = 100002;

int main() {

    int d[N];
    fill(d, d+N, 0);

    int n;
    cin >> n;

    rep(i, n) {
        int a, b;
        cin >> a >> b;
        d[a]++;
        d[b]--;
    }

    int ans=0;
    for (int i=1; i<N; ++i) {
        d[i] += d[i-1];
        ans = max(ans, d[i]);
    }

    cout << ans << endl;

}

C++: #include<iostream>
#include<algorithm>
using namespace std;
#define REP(i, a, n) for(int i=a; i<n; i++)
#define INF 1000000000

int main()
{
	int N;
	int a, b;
	int min_mem = 0, max_mem = INF;
	cin >> N;
	REP(i, 0, N) {
		cin >> a >> b;
		min_mem = max(min_mem, a);
		max_mem = min(max_mem, b);
	}

	if (min_mem - 1 <= N && min_mem <= max_mem) {
		cout << min_mem - 1 << endl;
	}
	else {
		cout << 0 << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<cstdio>
using namespace std;
int main()
{
  int friends[100005]={};
  int i;
  int n;
  int a,b;
  int max=0;

  cin>>n;
  for(i=0;i<n;i++){
    cin>>a>>b;
    friends[a]++;
    friends[b+1]--;
    //    cout<<"i"<<i<<endl;
  }
  for(i=1;i<100005;i++)
    friends[i]+=friends[i-1];
  for(i=0;i<100005;i++)
    if(friends[i]>=i-1)
      max=i-1;

  cout<<max<<endl;
}

C++: #include<stdio.h>
int bit[100001];
int n;
void add(int i,int x){
	while(i<=n){
		bit[i]+=x;
		i+=i&-i;
	}
}

int sum(int i){
	int s=0;
	while(i>0){
		s+=bit[i];
		i-=i&-i;
	}
	return s;
}

int main(){
	int i;
	scanf("%d",&n);
	for(i=0;i<=n;i++)bit[i]=0;
	for(i=0;i<n;i++){
		int a,b;
		scanf("%d %d",&a,&b);
		a--;b--;
		add(a,1);
		add(b+1,-1);
	}
	int ans=0;
	for(i=1;i<=n;i++)if(sum(i)>=i)ans=i;
	printf("%d\n",ans);
	return 0;
}

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

const int MAX_N=1e5+10;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin >> N;
    vector<int> cnt(MAX_N,0);
    for (int i=0;i<N;++i){
        int a,b; cin >> a >> b;
        ++cnt[--a]; --cnt[b];
    }
    for (int i=0;i<MAX_N-1;++i) cnt[i+1]+=cnt[i];
    int ans=0;
    for (int i=1;i<MAX_N;++i){
        if (i<=cnt[i]) ans=i;
    }
    cout << ans << '\n';
}

C++: #include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <cmath>
#include <climits>
#include <set>
using namespace std;
#define repn(i,s,n) for (int i=int(s); i < int(n); i++)
#define rep(i,n) repn(i,0,n)
#define repd(i,n) for (int i=int(n)-1; i >= 0; i--)
#define all(c) (c).begin(), (c).end()


int main() {
  int n;
  cin >> n;
  struct Friend {
    int a, b;
  };
  vector<Friend> fs(n);
  rep(i, n) cin >> fs[i].a >> fs[i].b;

  auto fs_a = fs;
  auto fs_b = fs;
  sort(all(fs_a), [](const Friend& a, const Friend& b) { return a.a < b.a; });
  sort(all(fs_b), [](const Friend& a, const Friend& b) { return a.b < b.b; });

  int num_can_take = 0;
  const int MAX_N = 100000;
  int fs_a_idx = 0;
  int fs_b_idx = 0;
  int ans = 0;

  rep(num_take, MAX_N+1) {
    while (fs_a_idx < n && fs_a[fs_a_idx].a-1 <= num_take) {
      fs_a_idx++;
      num_can_take++;
    }
    while (fs_b_idx < n && fs_b[fs_b_idx].b-1 < num_take) {
      fs_b_idx++;
      num_can_take--;
    }
    if (num_can_take >= num_take) ans = num_take;
  }
  cout << ans << endl;

  return 0;
}

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

#define rep(i,N) for(int i=0;i<(int)(N);++i)
#define rrep(i,N) for(int i=(int)(N)-1;i>=0;--i)
#define debug(x) cout<<#x<<"="<<x<<endl;
constexpr ll MOD=1000000007;
constexpr ll INF=1LL<<60;
template<class T> inline bool chmin(T& a,T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a,T b){if(a<b){a=b;return true;}return false;}
template<typename T> void fail(T v){cout << v << endl;exit(0);}
//template end

void solve(){
    int N;
    cin>>N;
    vector<int> imos(100100,0);
    rep(i,N){
        int a,b;cin>>a>>b;a--;
        imos[a]++;imos[b]--;
    }
    int res=0;
    rep(i,100010)imos[i+1]+=imos[i];
    rep(i,100010)if(imos[i]>=i)res=i;
    cout<<res<<endl;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);
    solve();
    return 0;
}

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

#define rep(i,n) for(int i = 0; i < (n); ++i)
#define ll long long

int main(void)
{
    ll n; cin>>n;
    vector<ll> v(1e7,0);
    ll ans=0;
    rep(i,n){
        ll a,b; cin>>a>>b;
        a--;
        v[a]++;
        v[b]--;
    }
    for(int i=1;i<=n;i++){
        v[i]+=v[i-1];
        if(v[i]>=i) ans=i;
    }
    cout<<ans<<endl;
    return 0;
}