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 N 100002
using namespace std;
int n,a,b,d[N],ans;

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

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

using namespace std;
int x[111111];
int y[111111];
int main(void){
  int i,n,m;
  int a,b;
  int res;
  cin>>n;
  res=m=0;
  for(i=1;i<=n;i++){
    cin>>a>>b;
    x[a]++;
    x[b+1]--;
    m=max(m,max(a,b));
  }
  m++;
  for(i=1;i<=m;i++){
    y[i]=x[i]+y[i-1];
    if(y[i]+1>=i) res=max(res,i);
  }
  /*
  for(i=1;i<=m;i++){
    cout<<x[i]<<" ";
  }
  cout<<endl;
  for(i=1;i<=m;i++){
    cout<<y[i]<<" ";
  }
  cout<<endl;
  */
  //if(res==0) cout<<0<<endl;
  cout<<res-1<<endl;
  return 0;
}

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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) begin(r),end(r)
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)
#define vvl(v,w,h,init) vector<vector<ll>> v(w,vector<ll>(h,init))
#define mp(a,b) make_pair(a,b)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

template<typename T>
void O(T t){
  cout << t << endl;
}

const ll mod = 1e9+7;
constexpr ll inf = ((1<<30)-1)*2+1 ;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0,1,-1,1,-1};
const int dx[] = {0,-1,0,1,1,-1,-1,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,a,b;
  cin >> n;
  vector<ll> ans(n+10);
  rep(i,n){
    cin >> a >> b;
    if(a) a--;
    if(b) b--;
    ans[a] ++;
    ans[b] --;
  }
  rep(i,n){
    ans[i+1] += ans[i];
  }
  ll k = 0;
  rep(i,n+1){
    k = max(k,ans[i]); 
  }
  if(k == 1) cout << 0 << endl;
  else
    cout << k << endl;
  return 0;
}

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

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

int main(){
  int n,ans[MAX],a,b;
  fill(ans,ans+MAX,0);

  cin >> n;
  while(n--){
    cin >> a >> b;
    for(int i = a ; i <= b ; i++)
      ++ans[i];
  }
  int fri = 0;
  for(int i = 0 ; i < MAX ; i++){
    if(ans[i]+1 >= i && ans[i]!=0) fri = ans[i]-1; 
  }
  cout << fri << endl;

  return 0;
}

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

bool data[100001][100002];

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

	//input
	for(int i = 1; i <= n; i++){
		int r, l;
		cin >> r >> l;
		for(int j = r; j <= l; j++) data[i][j] = true;
	}

	vector<int> v;
	for(int i = 1; i <= n; i++){
		int ok = 0;
		for(int j = 1; j <= n; j++){
			if(data[j][i]) ok++;
		}
		if(ok + 1 >= i) v.push_back(i);
	}

	if(v.size()){
		sort(v.begin(), v.end());
		cout << v[v.size() - 1] - 1 << endl;
	}
	else cout << 0 << endl;

	return 0;
}

C++: #include<iostream>
using namespace std;
int main(){
  int n,a,b,max=0,tmp;
  int imos[100003]={};
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> a >> b;
    imos[a]++;
    imos[b+1]--;
  }
  for(int i=1;i<=n+1;i++){
    imos[i]=imos[i]+imos[i-1];
  }
  for(int i=0;i<=n;i++){
    if(imos[i+1]<i) continue;
    if(imos[i+1]>i)imos[i+1]=i;
    max=(imos[i+1]<max)?max:imos[i+1];
  }
  cout << max<<endl;
}

C++: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>

#define INF 1e09

using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::fixed;
using std::pair;
using std::make_pair;
using std::min;
using std::max;
using std::string;
using std::vector;
using std::list;
using std::map;
using std::set; // insert erase find count
using std::queue;
using std::priority_queue;
using std::stack;

typedef vector<int> VI;
typedef vector<vector<int> > VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<int, pair<int, int> > PIII;

int n;

bool check(VVI& table, int number) {
    // number ?????????????????????
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (table[i][0] <= number && table[i][1] >= number) {
            count++;
        }
    }
    if (count >= number - 1) {
        return true;
    } else {
        return false;
    }
}

int main(void) {
    cin >> n;
    VVI table(n, VI(2));
    for (int i = 0; i < n; i++) {
        cin >> table[i][0] >> table[i][1];
    }

    int left = 1;
    int right = n + 1;

    while (right - left > 1) {
        int middle = (left + right) / 2;
        if (check(table, middle) == false) {
            right = middle;
        } else {
            left = middle;
        }
    }
    cout << left - 1 << endl;

    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <ctime>
#define INF 999999999
#define mod 1000000007
 
#define rep(i,n) for(int i=0;i<int(n);i++)
#define rrep(i,n) for(int i=(n);i>=0;i--)
#define REP(i,a,b) for(int i=(a);i<int(b);i++)
#define all(x) (x).begin(),x.end()
#define pb push_back
#define mp make_pair
#define MOD(x) (x%(mod))
using namespace std;
 
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef pair<int,int> pi;
 
int dx[4]={1,0,-1,0};
int dy[4]={0,1,0,-1};
bool debug=false;
/*---------------------------------------------------*/

int main(){
  int ans=0;
  int n,a,b;
  int imos[100005]={0};
  cin>>n;
  rep(i,n){
    scanf("%d%d",&a,&b);
    imos[a-1]++;
    imos[b]--;
  }
  REP(i,1,n+1){
    imos[i]+=imos[i-1];
    if(imos[i]>=i)ans=i;
  }
  printf("%d\n",ans);
  return 0;
}