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 int long long
#define rep(i,n) for(int i = 0;i < n;i++)
#define all(n) n.begin(),n.end()

const int INF = 1e9;

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

	vector<int> imos(100003, 0);
	rep(i, n) {
		int a, b;
		cin >> a >> b;
		a--; b--;
		imos[a]++;
		imos[b+1]--;
	}
	int ans = 0;
	int count = 0;
	rep(i, imos.size()) {
		count += imos[i];
		if (count >= i) {
			ans = max(i, ans);
		}
	}
	cout << ans << endl;

	return 0;
}

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

int main(){
    int n;
    while(cin >> n){
        int acc[100010] = {};
        rep(i,n){
            int a,b;
            cin >> a >> b;
            ++acc[a];
            --acc[b+1];
        }
        int x = 0;
        int ans = 0;
        rep(i,100005){
            x += acc[i];
            if(x >= i-1) ans = max(ans, x-1);
        }
        cout << ans << endl;
    }
}

C++: #include <stdio.h>

#define FRIEND_NUM 100001
int c[FRIEND_NUM*8+1];
int main(){
  int a,b,N,i;

  scanf("%d",&N);
  for(i=0; i<N; ++i){
    scanf("%d %d",&a,&b);
    for(; a<=b; ++a){
      ++c[a];
    }
  }
  for(a=N+1; a>=2;){
    --a;
    if(c[a+1] >= a){
      printf("%d\n",a);
      break;
    }
  }
}

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

using namespace std;
	

int main(){
	int N;
	vector<pair<int,int>> friends;
	vector<int> humans;
	vector<int> nokemono;

	//INPUT
	cin >> N;
	for(int i=0;i<N;i++){
		int a, b;
		cin >> a >> b;
		friends.push_back(make_pair(a,b));
	}
	//UPCOUNT & DOWNCOUNT
	for(int i=0;i<=N+1;i++){
		int count = 0;
		int minn = 999999;
		for(auto range:friends){
			if(i >= range.first && i <= range.second){
				count++;
				if(range.second < minn)
					minn = range.second;
			}
		}
		count++;
		humans.push_back(count);
		if(minn == 999999) minn = 0;
		nokemono.push_back(minn);
		count = 0;
	}

	for(int i=0;i<=N+1;i++){
		if(humans[i] < nokemono[i]) nokemono[i] = humans[i];
	}
	
	for(int i=N+1;i>=0;i--){
		//cout << i << ":" << humans[i] << "," << nokemono[i] << endl;
	}	
	
	//bool flag = true;
	for(int i=N+1;i>0;i--){
		if(humans[i] < i){
			//cout << i << endl;
			nokemono[i] = 0;
		}
		//if(nokemono[i] < humans[i]) nokemono[i] = 0;
		//cout << i << ":" << humans[i] << "," << nokemono[i] << endl;
		//cout << i << "," << humans[i]-1 << endl;
		//flag = false;
		//break;
	}
	
	/*if(flag)
		cout << 0 << endl;*/
	

	int maxn = 0;
	for(int i=0;i<=N+1;i++){
		if(nokemono[i] > maxn) maxn = nokemono[i];
	}
	
	cout << max(0,maxn-1) << endl;
	return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
const int MAX = 100002;

int main(){
  int n,ans[MAX],a,b;
  int MX = -1<<27;
  int MN = 1<<27;
  fill(ans,ans+MAX,0);

  cin >> n;
  while(n--){
    cin >> a >> b;
    MX = max(MX,b);
    MN = min(MN,a);
    ans[a]++; ans[b+1]--;
    for(int i = a ; i <= b ; i++) ans[i]++;
  }
  int fri = 0;
  for(int i = MN ; i <= MX+1 ; i++){
    if(ans[i]+1 > i) fri = i-1; 
  }

  cout << fri << 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+1];
    }

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

C++: #include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define REP(i,a,n) for(ll i=a;i<n;i++)
#define ALL(s) s.begin(),s.end()
#define P pair<ll,ll>
#define vl vector<ll>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvl vector<vector<ll>>
#define print(n) cout<<n<<endl
const int M=100010;
const int inf=1000000007;
const long long INF=1000000000000000007;
using ll=long long;
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int main(){
	int n,a[M],b[M],tmp=0;
	bool ok=false;
	cin>>n;
	rep(i,n)cin>>a[i]>>b[i],a[i]--,b[i]--;
	for(int tmp=n;tmp>0;tmp--){
		int ttmp=0;
		rep(i,n)if(a[i]<=tmp&&tmp<=b[i])ttmp++;
		if(tmp<=ttmp){
			ok=true;
			print(tmp);
			break;
		}
		if(ok)break;
	}
	if(!ok)print(0);
}

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

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

C++: #include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 10000000
using namespace std;
typedef pair<int,int> P;
int main(){
    vector<P> allow;
    int n;
    cin >> n;
    int tmp_min = INF;
    rep(i,n){
        int a,b;
        cin >> a >> b;
        //a--; b--;
        tmp_min = min(tmp_min,min(a,b));
        allow.push_back(P(a,b));
    }
    n++;
    int ans = 0;
    for(int i=tmp_min; i<=n; i++){
        int cnt = 0;
        rep(j,n-1){
            if(i >= allow[j].first && i<= allow[j].second) cnt++;
            //cout << cnt << endl;
        }
        ans = max(cnt,ans);
        //cout << i << " " << ans << endl;
    }
    cout << ans-1 << endl;
    return 0;
}

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

#define fs first
#define sc second
typedef pair<int,int> P;

int n;
P p[200100];

int main(){
  cin >> n;
  for(int i=0;i<n;i++)cin >> p[i].fs >> p[i].sc;
  p[n] = P(1,100001);
  sort(p,p+n+1);

  priority_queue<int, vector<int>, greater<int> > q;
  unsigned int ans = 1;

  for(int i=0;i<=n;i++){
    while(!q.empty() && q.top() < p[i].fs)q.pop();
    q.push(p[i].sc);    
    if(p[i].fs <= q.size() && q.size() <= q.top())ans = max(ans,q.size());
  }

  cout << ans-1 << endl;
}