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<climits>
int main()
{
    int imos[100003];
    for(int i = 0; i < 100003; i++)
    {
        imos[i] = 0;
    }
    int N;
    std::cin >> N;
    for(int i = 0; i < N; i++)
    {
        int a, b;
        std::cin >> a >> b;
        imos[a]++;
        imos[b + 1]--;
    }
    for(int i = 0; i < 100002; i++)
    {
        imos[i + 1] += imos[i];
    }
    int ninzu_max = INT_MIN;
    for(int i = 0; i < 100003; i++)
    {
        if(i - 1 <= imos[i])
        {
            ninzu_max = i;
        }
    }
    std::cout << ninzu_max - 1 << std::endl;
    return 0;
}

C++: #include <cstdio>

int n;
int plus[100005];
int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		int a,b;
		scanf("%d%d",&a,&b);
		plus[a]++;
		plus[b+1]--;
	}
	int sum=0,res=0;
	for(int i=2;i<=100001;i++){
		sum+=plus[i];
		if(sum+1>=i)res=i-1;
	}
	printf("%d\n",res);
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f(i,x,n) for(int i=x;i<(int)(n);++i)
#define all(x) (x).begin(),(x).end()
const int mod=1000000007,MAX=50005,INF=1<<29;

int main(){
    
    std::ifstream in("text.txt");
    std::cin.rdbuf(in.rdbuf());
    cin.tie(0);
    ios::sync_with_stdio(false);
    
    while(1){
        int N;cin>>N;
        vector<int> S(200000,0);
        
        for(int i=0;i<N;i++){
            int a,b;cin>>a>>b;
            S[a]++;
            S[b+1]--;
        }
        
        for(int i=1;i<200000;i++) S[i]+=S[i-1];
        
        int ans=0;
        
        for(int i=1;i<200000;i++){
            if(S[i]+1>=i) ans=i-1;
        }
        
        cout<<ans<<endl;
        
        break;
    }
    
}

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

int d[100010];

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

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

	int ans = 0, s = 1;
	for(int i = 1; i <= N + 1; i++) {
		s += d[i];
		if(s >= i) {
			ans = i - 1;
		}
	}
	cout << ans << endl;
}

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

using std::cin;
using std::cout;
using std::endl;

int main(void) {
	int N;
	cin >> N;
	std::vector<int> frimin(N);
	std::vector<int> frimax(N);
	for (int i = 0; i < N; i++) {
		cin >> frimin[i] >> frimax[i];
	}

	std::sort(frimin.begin(), frimin.end());
	std::sort(frimax.begin(), frimax.end());

	int ret = 0;
	int count = 1;
	int imin = -1;
	int imax = -1;
	for (int f = 2; f < N+1 && f < frimax[N-1]; f++) {
		for (int i = imin+1; frimin[i] == f; i++) {
			count++;
			imin++;
		}
		for (int i = imax+1; frimax[i] == f; i++) {
			count--;
			imax++;
		}

		if ( count >= f && count != 1 ) {
			ret = f-1;
		}
	}

	cout << ret << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

int main(){
    int n;
    cin >> n;
    vector<int> s(100010);
    s[0] += 1;
    s.back()-=1;
    rep(i, n) {
        int a, b;
        cin >> a >> b;
        s[a]++;
        s[b+1]--;
    }
    rep(i, s.size()-1) {
        s[i+1] += s[i];
    }
    repr(i, s.size()) {
        if(s[i] >= i) {
            cout << i-1 << endl;
            return 0;
        }
    }
    cout << 0 << endl;
    return 0;
}

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

int a[100000], b[100000];
int main() {
	int n; scanf("%d", &n);
	rep(i, n)scanf("%d%d", &a[i], &b[i]);
	for (int i = n; i >= 0; i--) {
		int cnt = 0;
		for (int j = 0; j < n&&cnt < i; j++) {
			if (a[j] - 1 <= i&&i < b[j])cnt++;
		}
		if (cnt == i) {
			printf("%d\n", i);
			return 0;
		}
	}
	puts("0");
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n; cin>>n;
  int kemono[100001]={};
  for(int i=0;i<n;++i){
    int a,b; cin>>a>>b;
    for(int j=a;j<=b;++j) kemono[j]++;
  }
  int ans=0;;
  for(int i=0;i<100001;++i){
    if(i<=n)
      ans = max(ans,kemono[i]);
  }
  if(ans) cout<<ans-1<<endl;
  else cout<<0<<endl;
  return 0;
}

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

#define REP(i,a) for(long i=0;i<(a);++i)

typedef std::vector<int>  VI;

int N;
VI imos(100002, 0);
int main() {
    std::cin >> N;

    REP(i, N) {
        int a, b;
        std::cin >> a >> b;
        ++imos[a];
        --imos[b];
    }

    int x = 0, ans = 0;
    for(int i=0; i<=N+1; ++i) {
        x += imos[i];
        if(x + 1 >= i) {
            ans = i - 1;
        }
    }
    std::cout << ans << std::endl;
    return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<cassert>
using namespace std;
int main(){
  int n,st,ed;
  int info[100100][2],total,lim; // 0 -> st,1 -> ed
  while(cin >> n){
    total = 0; lim = 1;
    for(int i=0;i<100100;i++)info[i][0] = info[i][1] = 0;
    info[0][0] = info[1][0] = 1;
    for(int i=0;i<n;i++){
      cin >> st >> ed; lim = max(lim,ed);
      info[st][0]++;
      info[ed][1]++;
    }
    int ans = 0;
    for(int i=1;i<=lim;i++){
      if(info[i][0] != 0 && total+info[i][0] >= i)total+=info[i][0];
      if(info[i][1] != 0 && total > i){
	//total -= (total-i); 
	total -= info[i][1];
      }
     
      assert(total>=0);
      ans = max(ans,total);
    }

    cout << ans-1 << endl;

  }
  return 0;
}

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

int main() {
  int N, a, b, res;
  signed int invite[100000] = {0};

  cin >> N;
  for(int i = 0; i < N; i++) {
    cin >> a >> b;
    invite[a-1]++;
    invite[b]--;
  }

  signed int sum = 0;
  for(int i = 0; i < 100001; i++) {
    sum += invite[i];
    if(i <= sum)
      res = i;
  }
  cout << res << endl;
}