当前位置: 首页 > 工具软件 > Forban > 使用案例 >

C - Chat Ban

申昌勋
2023-12-01

C - Chat Ban
注意二分边界条件吧

// Problem: C. Chat Ban
// Contest: Codeforces - Educational Codeforces Round 117 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1612/problem/C
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include<bits/stdc++.h> 
using namespace std;
#define ff first
#define ss second
#define lowbit(x) (x&-x)
#define pf(a) printf("%d\n",a)
#define mem(x,y) memset(x,y,sizeof(x))
#define dbg(x) cout << #x << " = " << x << endl
#define rep(i,l,r) for(int i = l; i <= r; i++)
#define fep(i,a,b) for(int i=b; i>=a; --i)
#define PII pair<int,int>
//#define long long in 
typedef long long ll;

const int N = 1e6+10;

ll k,x;


void solve()
{
	scanf("%lld %lld",&k, &x);
	ll ans = 0;
	if(x>(1+k)*k/2) {
		ans += k;
		x -= (1+k)*k/2;
		ll l = 1, r = k-1; 
		while(l<=r){
			ll mid = (l+r)>>1;
			if((k-1+mid)*(k-mid)/2>=x) l = mid + 1;
			else r = mid-1;
		}
		ans += (k-r);
	}else{
		ll l = 1, r = k;
		while(l<=r){
			ll mid = (l+r)>>1;
			if((1+mid)*mid/2>=x) r = mid - 1;
			else l = mid + 1;
		}
		ans = l;
	}
	
	printf("%lld\n",min(ans,2*k-1));
}

int main()
{
	int t;
	scanf("%d", &t);
	while(t--)
		solve();
	return 0;
}

 类似资料: