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 <algorithm>

int imos[100010];

using namespace std;


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

  int ans = 0;
  imos[0] = 1;
  for (int i = 1; i < 100010; i++) {
    int t = 0;
    if (imos[i] == -1) {
      t = 1;
    }
    imos[i] += imos[i - 1];
    if (imos[i] - t >= i) {
      ans = max(ans, imos[i]);
    }
  }

  cout << ans - 2 << endl;

  return 0;
}

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 range:nokemono){
		cout << range << endl;
	}
	*/
	
	for(int i=N+1;i>0;i--){
		if(humans[i] < i) continue;
		if(nokemono[i] < humans[i]) continue;
		cout << i << endl;
		break;
	}
	return 0;
}

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<")"<< endl
#define BINARY(x) static_cast<bitset<16> >(x)

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

#define in_range(x, y, w, h) (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()

typedef long long ll;
const int INF   = 1e9;
const ll  INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};

int main()
{
    ios::sync_with_stdio(false);

    int N;
    cin >> N;

    const int MAX = 1e5 + 1;
    int sum[100010] = {};
    sum[0]++;
    sum[MAX+1]--;

    rep(i, N) {
        int A, B;
        cin >> A >> B;
        sum[A]++;
        sum[B+1]--;
    }

    rep(i, MAX+1) {
        sum[i+1] += sum[i];
    }

    int ans = 0;
    rep(i, MAX+1) {
        if (sum[i] >= i) {
            ans = i;
        }
    }

    cout << ans - 1 << endl;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
 
int main() {
  const int MAX = 100001;
  int n, a, b;
  int dp[MAX+1] = {};
   
  cin >> n;
  for(int i=0; i<n; i++) {
    cin >> a >> b;
    dp[a] ++;
    dp[b+1] --;
  }
 
  for(int i=2; i<MAX; i++) {
    dp[i+1] += dp[i];
  }
  
  int mx = 0;
  for(int i=2; i<MAX; i++) {
    if(dp[i]+1>=i) mx = max(mx, dp[i]);
  }
  cout << mx << endl;
   
  return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

static int member[100004];

void solve()
{
  int n, a, b;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a >> b;
    member[a]++;
    member[b + 1]--;
  }
  partial_sum(begin(member), end(member), begin(member));
  for(int i = 1e6 + 1; i > 0; i--) {
    if(member[i] >= i - 1) {
      cout << i - 1 << "\n";
      return;
    }
  }
}

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

C++: #include<iostream>
#include<vector>
using namespace std;
#define MAX 100005
int main(){
    int n;
    cin >> n;
    vector<int> imos(MAX);
    imos[0]++;
    for(int i = 0; i < n; i++){
        int a,b;
        cin >> a >> b;
        imos[a]++;
        imos[b+1]--;
    }
    for(int i = 0; i < MAX-1; i++)imos[i+1] += imos[i];
    for(int i = MAX-1; i > 0; i--){
        if(imos[i] >= i){
            cout << i-1 << endl;
            return 0;
        }
    }

}

C++: #include <bits/stdc++.h>
#define REP(i, n) for(int i = 0;i < n;i++)
using namespace std;
int main(){
    int n;
    int cnt[100100];
    memset(cnt,0,sizeof(cnt));
    cin>>n;
    REP(i,n){
        int a,b;
        cin>>a>>b;
        cnt[a]++;cnt[b+1]--;
    }
    int ans=0;
    REP(i,100000)cnt[i+1]+=cnt[i];
    REP(i,n+1){
        if(i==0)continue;
        if(cnt[i]+1>=i){
            ans=i-1;
        }
    }
    cout<<ans<<endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int N,a,b,ans;
  int max=0;
  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=0;i<N;i++){
    if(man[i]>max && man[i]>=i-1){
      max=man[i];
      ans=i;
    }
  }
  cout<<ans<<endl;
  return 0;
}

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

using namespace std;

int main(){
    int n,a,b;
    map<int,int> m;

    cin >> n;
    for(int i=0;i<n;i++){
        cin >> a >> b;
        m[a]++;
        m[b]--;
    }
    map<int,int>::iterator it = m.begin(), jt;
    int cnt=1;
    int ret=-1;
    while(it != m.end()){
        cnt += (*it).second;
        jt = it;
        jt++;
        if(jt == m.end() || (*it).first <= cnt && cnt <= (*jt).first){
            ret = max(ret,cnt);
        }
        ++it;
    }
    cout << ret-1 << endl;
}

C++: #include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
#define REP(i,n) for(int i=0;i<(n);i++)
#define MP make_pair
#define PII pair<int,int>

int main()
{
	int N;
	int ans = 1;
	cin >> N;
	vector <PII> friends;
	queue <PII> q;
	REP(i, N)
	{
		int t1, t2;
		cin >> t1 >> t2;
		friends.push_back(MP(t1, t2));
	}
	sort(friends.begin(), friends.end());
	REP(i, N)
	{
		if (q.empty())
		{
			q.push(friends[i]);
			if (q.front().first <= q.size())
			{
				ans = max(ans, int(q.size()));
			}
		}
		else if (friends[i].first > N + 1)continue;
		else if (q.front().second < friends[i].first)
		{
			while (1)
			{
				if (q.empty())break;
				if (q.front().second >= friends[i].first)break;
				q.pop();
			}
			q.push(friends[i]);
		}
		else if (q.front().second == friends[i].first)
		{
			q.pop();
			q.push(friends[i]);
		}
		else
		{
			q.push(friends[i]);
			if (q.front().first <= q.size())
			{
				ans = max(ans, int(q.size()));
			}
		}
	}
	cout << ans-1 << endl;
	return 0;
}

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

#define INF 1e9
#define llINF 1e18
#define MOD 1e9+7
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
using namespace std;
int main(){
  int n;cin>>n;
  int rui[100010]={};
  for(int i=0;i<n;i++){
    int a,b;cin>>a>>b;
    rui[a]++;
    rui[b+1]--;
  }
  int ma=1;
  int now=1;
  for(int i=2;i<100010;i++){
    now+=rui[i];
    //cout<<now<<endl;
    if(now>=i){
    ma=max(ma,i);
    }
  }
  
  cout<<ma-1<<endl;
  return 0;
}