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<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=i-(lower_bound(vec.begin(),vec.end(),i)-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>
using namespace std;

int n,a,b;
int A[100002]={},B[100002]={};

int main()
{
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>a>>b;
    A[a]++;
    B[b]++;
  }
  int sum=1,ans=0;
  for(int i=1;i<100002;i++){
    sum+=(A[i]-B[i]);
    //if(i<=10)cout<<i<<" "<<sum<<endl;
    if(i<=sum)ans=i;
  }
  cout<<ans-1<<endl;
  return 0;
}

C++: //https://onlinejudge.u-aizu.ac.jp/#/problems/2331
//imos法
#include<vector>
#include<iostream>
#include<algorithm>
#define MAX_N 100010
using namespace std;
long long v[MAX_N];
long long ans[MAX_N];
int main()
{
	int K;//友達の総数
	cin >> K;
	for (long long i = 0; i < K; i++)
	{
		long long  a, b;
		cin >> a >> b;
		v[a] = v[a] + 1;
		v[b + 1]=v[b+1]-1;
		
	}
	for (long long i = 1; i < MAX_N ; i++)
	{
		v[i] = v[i] + v[i - 1];
	}
	long long x = 0;
	for (long long i = 0; i < MAX_N; i++)
	{
		
		if (i <= (v[i] + 1))
		{
			x = max(x, i);
		}
	}
	cout << x -1<< endl;
	
	return 0;
}

C++: #include <iostream>

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

using namespace std;


int main(){

	int n;
	int *a, *b;
	int *data;
	cin >> n;

	a = new int[n];
	b = new int[n];
	data = new int[100010];

	REP(i, n){
		cin >> a[i] >> b[i];
	}
	REP(i, 100010){
		data[i] = 0;
	}
	REP(i, n){
		data[a[i]]++;
		data[b[i]+1]--;
	}
	int num = 0;
	REP(i, 10){
	//	cout << data[i] << " ";
	}
	//cout << endl;
	REP(i, 10){
		num += data[i];
		data[i] = num;

	//	cout << data[i] << " ";
	}
	//cout << endl;

	for (int i = n + 1; i > 0; i--){
		if (data[i]+1 >= i){
			cout << i - 1 << endl;
			//cout << "Ohuotn" << endl;
			return 0;
		}
	}
	cout << 0 << endl;


	//cout << "Ohuotn" << endl;

	return 0;
}

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

int a[100005] = {};

int main(void)
{
	int max;
	cin >> max;
	a[0]++;
	for(int i=0;i<max;++i){
		int c,b;
		cin >> c >> b;
		a[c-1]++;a[b]--;
	}
	int sum=0;
	int maxn=1;
	for(int i=0;i<=max;++i){
		sum+=a[i];
		if(i <= sum)
		{
			maxn = i;
		}
	}
	cout << maxn-1 << endl;
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0; i<(int)(n); i++)
#define iter(c) __typeof((c).begin())
#define tr(it,c) for(iter(c) it=(c).begin(); it!=(c).end(); it++)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define pr(a) cout << (a) << endl
#define F first
#define S second
typedef long long ll;
typedef pair<int,int> P;
const int MAX=1000000001;
int dx[4]={-1,0,1,0},dy[4]={0,-1,0,1};

int main() {
  int n;
  cin >> n;
  P a[100005];
  memset(a,0,sizeof(a));
  rep(i,n) {
    int x,y;
    cin >> x >> y;
    a[x].F++;
    a[y].S++;
  }
  int ans=0,p=1;
  for(int i=1; i<100005; i++) {
    p+=a[i].F;
    p-=a[i-1].S;
    if(p>=i) ans=i-1;
  }
  pr(ans);
  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[b+1]--;
    for(int i = a ; i <= b ; i++) ans[i]++;
  }
  int fri = 0;
  for(int i = MN ; i <= MX+2 ; i++){
    if(ans[i] >= i) fri = i-1; 
  }

  cout << fri << endl;

  return 0;
}

C++: #include <iostream>

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

using namespace std;


int main(){

	int n;
	int *a, *b;
	int *data;
	cin >> n;

	a = new int[n];
	b = new int[n];
	data = new int[100010];

	REP(i, n){
		cin >> a[i] >> b[i];
	}
	REP(i, 100010){
		data[i] = 0;
	}
	REP(i, n){
		data[a[i]]++;
		data[b[i]+1]--;
	}
	int num = 0;
	REP(i, 10){
	//	cout << data[i] << " ";
	}
	//cout << endl;
	REP(i, 100010){
		num += data[i];
		data[i] = num;

	//	cout << data[i] << " ";
	}
	//cout << endl;

	for (int i = n + 1; i > 0; i--){
		if (data[i]+1 >= i){
			cout << i - 1 << endl;
			//cout << "Ohuotn" << endl;
			return 0;
		}
	}
	cout << 0 << endl;


	//cout << "Ohuotn" << endl;

	return 0;
}

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

using namespace std;

const int N = 100001;

int cnt[2][N+1];

main(){
  int n;
  cin >> n;
  fill(cnt[0], cnt[2], 0);
  for(int i=0;i<n;i++){
    int a, b;
    cin >> a >> b;
    cnt[0][a]++;
    cnt[1][b]++;
  }
  int ans = 0;
  int sum = 1;
  for(int i=0;i<N+1;i++){
    sum -= cnt[1][i];
    sum += cnt[0][i];
    if(sum >= i) ans = max(ans, i - 1);
  }
  cout << ans << endl;
}

C++: //08
#include<iostream>

using namespace std;

int main(){
  int n;
  cin>>n;
  int p[100003]={};
  for(int i=0;i<n;i++){
    int a,b;
    cin>>a>>b;
    p[a]++;
    p[b+1]--;
  }
  int c=1,r;
  for(int i=0;i<=100001;i++){
    c+=p[i];
    if(c>=i){
      r=i;
    }
  }
  cout<<r-1<<endl;
  return 0;
}

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 = 0;
	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]);
			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
		{
			q.push(friends[i]);
			ans = max(ans, int(q.size()));
		}
	}
	if (N == 1 && friends[0].first == 2)cout << 1 << endl;
	else cout << ans - 1 << endl;
	return 0;
}