C. Keshi Is Throwing a Party
我们显然可以二分答案
我们的最优解情况就是从小到大的选择
要是a[i]>=x-cnt-1(还要减去自身)&&b[i]>=cnt我们就把他算进去
这样肯定是最优的 当然从大到小也可以
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int M = 998244353;
const int mod = 20220911;
#define int long long
int up(int a,int b){return a<0?a/b:(a+b-1)/b;}
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);
int n,a[N],b[N];
bool check(int x){
int cnt=0;
for(int i=1;i<=n;i++){
if(cnt<=b[i]&&x-cnt-1<=a[i])++cnt;
if(cnt==x)return 1;
}
return 0;
}
void solve() {
cin>>n;
for(int i=1;i<=n;i++)cin>>a[i]>>b[i];
int l=1,r=n;
while(l<r){
int mid=l+r+1>>1;
if(check(mid))l=mid;
else r=mid-1;
}
cout<<l<<endl;
}
signed main(){
fast
int t;t=1;cin>>t;
while(t--) {
solve();
}
return ~~(0^_^0);
}
标签:cnt,const,17,int,Global,Codeforces,long,mid
From: https://www.cnblogs.com/ycllz/p/16795195.html