CodeForces-1288 A Deadline

See this article on

A. Deadline
time limit per test
2 seconds
memory limit per test
256 megabytes
input: standard input
output: standard output

Adilbek was assigned to a special project. For Adilbek it means that he has nn days to run a special program and provide its results. But there is a problem: the program needs to run for dd days to calculate the results.

Fortunately, Adilbek can optimize the program. If he spends xx (xx is a non-negative integer) days optimizing the program, he will make the program run in dx+1⌈\frac{d}{x+1}⌉days (a⌈a⌉ is the ceiling function: 2.4=3⌈2.4⌉=3,2=2⌈2⌉=2). The program cannot be run and optimized simultaneously, so the total number of days he will spend is equal to x+dx+1x+⌈\frac{d}{x+1}⌉.

Will Adilbek be able to provide the generated results in no more than nn days?


The first line contains a single integer TT $(1≤T≤50) $— the number of test cases.

The next TT lines contain test cases – one per line. Each line contains two integers nn and dd (1n109,1d109)(1≤n≤109, 1≤d≤109) — the number of days before the deadline and the number of days the program runs.


Print TT answers — one per test case. For each test case print YESYES (case insensitive) if Adilbek can fit in nn days or NONO (case insensitive) otherwise.



1 1
4 5
5 11




In the first test case, Adilbek decides not to optimize the program at all, since dnd≤n.

In the second test case, Adilbek can spend 11 day optimizing the program and it will run 52=3⌈\frac{5}{2}⌉=3 days. In total, he will spend 4 days and will fit in the limit.

In the third test case, it’s impossible to fit in the limit. For example, if Adilbek will optimize the program 2 days, it’ll still work 112+1=4⌈\frac{11}{2+1}⌉=4 days.


First of all I tried the violent way. Time limit exceeded. So I tried mathematic way.
y=x+dx+1y=1d(x+1)2 y=x+\frac{d}{x+1} \\ \Rightarrow y^{'}=1-\frac{d}{(x+1)^2}
When y=0y^{'}=0 , y reaches it’s extremum. So:
x=d1(x>0)ymax=2d1 x=\sqrt{d}-1(x>0) \\ y_{max}=2\sqrt{d}-1
y is a integer, so y=[2d1]y=[2\sqrt{d}-1]


using namespace std;
int main(){
	int t;
	cin >> t;
	while(t--) {
		int n, d, flag = 1;
		cin >> n >> d;
		if(2*sqrt(d)-1 <= n) {
			cout << "YES" << endl;
		} else {
			cout << "NO" << endl;
	return 0;
发布了87 篇原创文章 · 获赞 0 · 访问量 1667


©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客