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>
#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<int> allow(100001,0);
    int n;
    cin >> n;
    rep(i,n){
        int a,b;
        cin >> a >> b;
        allow[a-1]++;
        allow[b]--;
    }
    int ans;
    for(int i=0; i<=n; i++){
        if(i) allow[i] += allow[i-1];
        if(allow[i] >= i) ans = i;
    }
    cout << ans << endl;
    return 0;
}

C++: #include<iostream>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
using namespace std;

int main(){
	int n,a,b,ans=-1;
	cin>>n;
	int dt[100002]={};
	rep(i,n){
		cin>>a>>b;
		dt[a]++;
		dt[b+1]--;
	}
	int cnt=-1;
	loop(i,1,100002){
		dt[i]+=dt[i-1];
	}
	rep(i,n+1){
		if(ans<dt[i])ans=dt[i];
	}
	if(n!=1)cout<<ans-1<<endl;
	else if(dt[2]!=0)cout<<1<<endl;
	else cout<<0<<endl;
	return 0;
}

C++: #include<iostream>
#include<iomanip>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<queue>
#include<vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

#define MAX 100002

int main() {
	int imos[MAX] = {};
	int N; cin >> N;

	for (int i = 0; i < N; i++) {
		int a, b; scanf("%d%d", &a, &b);
		imos[a]++; imos[b + 1]--;
	}
	imos[0] = 1;
	for (int i = 1; i < MAX; i++) {
		imos[i] += imos[i - 1];
	}
	int ans = 0;
	for (int i = 0; i < MAX; i++) {
		if (imos[i] >= i)ans = max(ans, i - 1);
	}
	cout << ans << endl;
	return 0;
}

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));
    }
    int tmp_max = 0;
    int ans = 0;
    for(int i=tmp_min; i<=n; i++){
        int cnt = 0;
        rep(j,n){
            if(i >= allow[j].first && i<= allow[j].second) cnt++;
            //cout << cnt << endl;
        }
        if(tmp_max <= cnt && i <= cnt) {
            tmp_max = cnt;
            ans = i;
        }
    }
    cout << ans << endl;
    return 0;
}

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

using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define MP make_pair
#define PB push_back
#define MOD 1000000007
#define INF (1LL<<30)
#define LLINF (1LL<<60)
#define PI 3.14159265359
#define EPS 1e-12
#define int ll

int cnt[100010];
signed main(void)
{
  int n;
  cin >> n;
  REP(i, n) {
    int a, b;
    cin >> a >> b;
    cnt[a]++;
    cnt[b]--;
  }
  FOR(i, 1, 100001) cnt[i] += cnt[i-1];
  //REP(i, 10) cout << cnt[i] << " "; cout << endl;
  int ret = 0;
  REP(i, n+1) {
    //cout << cnt[i] << " " << i << endl;
    if(cnt[i] >= i-1) {
      ret = i-1;
    }
  }
  cout << ret << 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;
  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;
}

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

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<int> v(100000 + 2 + 1, 0);
    for (int i = 0; i < n; ++i) {
        int a;
        int b;
        cin >> a >> b;
        // ?????????????¬?????????§1?????????
        v[b + 1] -= 1;
        // ???????????§1????¢???????
        v[a] += 1;
    }

    // Scan ????????¨???????????°???????????????
    for (int i = 1; i < v.size() - 1; ++i) {
        v[i] = v[i - 1] + v[i];
    }
    int max_{0};
    for (int i = 0; i < v.size(); ++i) {
        // ????????????????????¨????????????, ????????????????????°???????????????????????£?????????
        if (0 < v[i] && (i - 1) <= v[i]) {
            max_ = i;
        }
    }
    if (max_ != 0) {
        cout << (max_ - 1) << endl;
    }
    else {
        cout << max_ << endl;
    }
}

C++: #include<iostream>
#include<map>
#include<algorithm>
#include<functional>
#define all(c) c.begin(),c.end()
#include<vector>
#define MAX 100010
using namespace std;
typedef pair<int,int> P;
int main(){
	int n;
	cin>>n;
	int ans=0;
	//int a[MAX],b[MAX];
	int a,b;
	vector<P> box;
	for(int i=0;i<n;i++){
		//cin>>a[i]>>b[i];
		cin>>a>>b;
		box.push_back(P(a,b));
	}
	sort(all(box));
	for(int i=1;i<=n+1;i++){
		vector<int> vec;
		for(int j=0;j<n;j++){
			if(i>=box[j].first){
				vec.push_back(box[j].second);
				
				
			}else
			break;
		}
		//cout<<i<<endl;
		sort(all(vec));
		int t=0;
		for(int k=0;vec.size()>k;k++){
			if(i<=vec[k]){
			t++;
			//cout<<"------"<<endl;
			}
		}
		
		int s=(lower_bound(vec.begin(),vec.end(),i,greater<int>())-vec.begin());
		cout<<t<<"   "<<s<<endl;
		if(t+1<i)
		continue;
		ans=max(ans,i-1);
		if(t==0)
		ans=max(ans,0);
		for(int k=0;k<(int)vec.size();k++)
			cout<<vec[k];
		cout<<endl;
		//cout<<"  "<<t<<endl;
		
		vec.clear();
	}
	
	cout<<ans<<endl;
	
	return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,x,n) for(int i=(x);i<(n);i++)
#define all(v) (v).begin(),(v).end()
#define int long long
using namespace std;
const int MOD=1e9+7;
const int INF=1e9;
const double ESP=1e-5;
template<typename T> void cmax(T &a, T b) {a = max(a, b);}
template<typename T> void cmin(T &a, T b) {a = min(a, b);}

int d[100005];
signed main(){
  int n;
  cin>>n;
  rep(i,n){
    int a,b;
    cin>>a>>b;
    a--;b--;
    d[a]++;
    d[b]--;
  }
  int ans=0;
  rep(i,100000)d[i+1]+=d[i];
  loop(i,1,100001)if(d[i]>=i)ans=i;
  cout<<ans<<endl;
 return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#define all(c) c.begin(),c.end()
#include<vector>
#define MAX 100010
using namespace std;
int main(){
	int n;
	cin>>n;
	int ans=0;
	int a[MAX],b[MAX];
	for(int i=0;i<n;i++)
		cin>>a[i]>>b[i];
	for(int i=1;i<=n+1;i++){
		vector<int> vec;
		for(int j=0;j<n;j++){
			if(i>=a[j]){
				vec.push_back(b[j]);
			}
		}
		//cout<<i<<endl;
		sort(all(vec));
		int t=vec.size()-(lower_bound(vec.begin(),vec.end(),i)-vec.begin());
		ans=max(ans,t);
		/*for(int k=0;k<(int)vec.size();k++)
		cout<<vec[k];
		cout<<endl;
		cout<<"  "<<t<<endl;
		vec.clear();*/
	}
	
	cout<<ans-1<<endl;
	
	return 0;
}