COFO::1610C Keshi Is Throwing a Party

Problem

Point

Design

Complexity

Code

#include<bits/stdc++.h>
#define all(a) a.begin(), a.end()
#define sz(a) (int) a.size()
#define rep(i, a, b) for(int i=(a); i <(b); i++)
#define r_rep(i, a, b) for(int i=(a); i >(b); i--)
typedef long long ll;

using namespace std;
const int MAXN = 2 * 1e5 + 9;

int n;
int a[MAXN], b[MAXN];

void bs(ll st, ll en) {
    
    auto possible = [](ll x) {
        ll cnt = 0;
        rep(i, 0, n) {
            if(a[i] >= x - 1 - cnt && b[i] >= cnt) cnt++;
        }
        return cnt >= x;
    };
    
    ll ans = 0;
    while(st <= en) {
        ll mid = (st + en) >> 1;
        if(possible(mid)) {
            ans = max(ans, mid);
            st = mid + 1;
        } else en = mid - 1;
    }
    cout << ans << '\n';
}
void solve() {
    cin >> n;
    rep(i, 0, n) cin >> a[i] >> b[i];
    
    bs(0, n);
}
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int tc; cin >> tc;
    while(tc--)
        solve();
    return 0;
}