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>
using namespace std;
using vi=vector<int>;
using vvi=vector<vi>;
using vs=vector<string>;
using msi=map<string,int>;
using mii=map<int,int>;
using pii=pair<int,int>;
using vlai=valarray<int>;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
int main(void){
    int n,w,h;
    cin>>n>>w>>h;
    vi wl(w+1,0),hl(h+1,0);
    rep(i,n){
        int x,y,w;
        cin>>x>>y>>w;
        wl[x-w]++;
        wl[x+w]--;
        hl[y-w]++;
        hl[y+w]--;
    }
    partial_sum(all(wl),wl.begin());
    partial_sum(all(hl),hl.begin());
    cout<<(count(all(wl),0) || count(all(hl),0)?"No":"Yes")<<endl;
}

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

using namespace std;

typedef long long ll;
typedef pair<int,int> pii;
typedef vector<int> VI;
typedef vector<VI> VVI;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = numeric_limits<int>::max() / 2;
const int NEG_INF = numeric_limits<int>::min() / 2;


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

  int n;cin>>n;
  int arr[100010]={};
  for(int i=0;i<n;i++){
      int a,b;cin>>a>>b;
      arr[a]++;arr[b+1]--;
  }
  for(int i=1;i<100010;i++){
      arr[i]+=arr[i-1];
  }
  for(int i=100000;1<=i;i--){
      if(arr[i]>=i-1){
          cout<<i<<endl;
          return 0;
      }
  }
  cout<<0<<endl;
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=1;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int main(){
  int n;
  cin>>n;
  int a[100002];
  lp(i,100002){
    a[i]=1;
  }
  lp(i,n){
    int x,y;
    cin>>x>>y;
    for(int i=x;i<=y;i++){
      a[i]+=1;
    }
  }
  int ans=0;
  lp(i,n+2){
    if(a[i]>=i) ans=i;
  }
  if(ans==0) cout<<0<<endl;
  else 
  cout<<ans-1<<endl;
  return 0;
    
}

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

int main(){
  int N, a, b, ans, s, t;
  cin >> N;
  vector<int> A, B;
  for (int i = 0; i < N; ++i) {
    cin >> a >> b;
    A.push_back(a);
    B.push_back(b);
  }
  sort(A.begin(),A.end());
  sort(B.begin(),B.end());
  ans = 0;
  vector<int>::iterator itr1, itr2;
  for (int i = N + 1; i > 0; --i) {
    itr1 = lower_bound(A.begin(),A.end(),i+1);
    itr2 = lower_bound(B.begin(),B.end(),i);
    s = distance(A.begin(), itr1);
    t = distance(B.begin(), itr2);
    if (s-t >= i-1) {
      ans = i-1;
      break;
    }
  }
  cout << ans << endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main(){
  int n,s[123456]={},a,b,ans;
  cin>>n;
  while(n--){
    cin>>a>>b;
    s[a-1]++,s[b]--;
  }
  for(int i=0;i<100001;s[++i]+=s[i-1])
    if(i<=s[i])ans=i;
  cout<<ans<<endl;
  return 0;
}

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

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

  cout << ans << endl;

  return 0;
}

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

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

int n, a, b, friends[100005], fri, ans;

/***************************************

           imos???version

***************************************/

int main() {
  std::cin.tie(0);
  std::ios::sync_with_stdio(false);

  std::cin >> n;
  rep(i,n) {
    std::cin >> a >> b;
    a--;
    friends[a]++;
    friends[b]--;
  }

  for (int i = 1; i <= n; ++i) friends[i] += friends[i-1];

  rep(i,n+1) {
      if (friends[i] >= i) {
          ans = std::max(ans, i);
      }
  }

  std::cout << ans << std::endl;

  return 0;
}

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

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

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

using namespace std;

bool predication(pair<int,int> arg1, pair<int,int> arg2){
    if(arg1.first == arg2.first){
        return arg1.second > arg2.second;
    }
    return arg1.first < arg2.first;
}

int main(){
    int n,a,b;
    vector<pair<int,int> > v;

    cin >> n;
    for(int i=0;i<n;i++){
        cin >> a >> b;
        v.push_back(make_pair(a,1));
        v.push_back(make_pair(b,-1));
    }
    sort(v.begin(),v.end(),predication);
    int cnt=1;
    int ret=-1;
    for(int i=0;i<v.size();i++){
        cnt += v[i].second;
        if(i+1==v.size() || v[i].first <= cnt && cnt <= v[i+1].first){
            ret = max(ret, cnt);
        }
    }
    cout << ret-1 << endl;
}

C++: #include<iostream>
#include<vector>
using namespace std;
int main(){
    int n;
    cin >> n;
    vector<int> a(n),b(n);
    for(int i = 0; i < n; i++)cin >> a[i] >> b[i];
    int ok = 0, ng = n+1;
    int t = 200;
    while(t--){
        int mid = (ok + ng)/2;
        int cnt = 0;
        for(int i = 0; i < n; i++){
            if(a[i] <= mid+1 && mid+1 <= b[i])cnt++;
        }
        //cout << ok << " " << ng << " " << mid << " " << cnt << endl;
        if(cnt >= mid)ok = mid;
        else ng = mid;
    }
    cout << ok << endl;
}

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

using namespace std;

int main() {
    int N = 0;
    cin>>N;
    vector<pair<int,int> > frnds(N);
    for (int n = 0; n < N; n++) {
        int a,b;
        cin>>a>>b;
        frnds[n] = make_pair(a,b);
    }
    sort(frnds.begin(), frnds.end());
    for (int max = N + 1; max >= 1; max--) {
        int miss = 0;
        for (int n = N-1; n>= 0; n--) {
            if (!(frnds[n].first <= max && max <=frnds[n].second)) {
                miss++;
                if (N - miss + 1 < max) break;
            }
        }
        if (N - miss + 1 >= max) {
            cout<<max-1<<endl;
            break;
        }
    }
}