A. Find The Array

题目:https://codeforces.com/contest/1550/problem/A

Let’s call an array a consisting of n positive (greater than 0) integers beautiful if the following condition is held for every i from 1 to n: either ai=1, or at least one of the numbers ai−1 and ai−2 exists in the array as well.

For example:

the array [5,3,1] is beautiful: for a1, the number a1−2=3 exists in the array; for a2, the number a2−2=1 exists in the array; for a3, the condition a3=1 holds;
the array [1,2,2,2,2] is beautiful: for a1, the condition a1=1 holds; for every other number ai, the number ai−1=1 exists in the array;
the array [1,4] is not beautiful: for a2, neither a2−2=2 nor a2−1=3 exists in the array, and a2≠1;
the array [2] is not beautiful: for a1, neither a1−1=1 nor a1−2=0 exists in the array, and a1≠1;
the array [2,1,3] is beautiful: for a1, the number a1−1=1 exists in the array; for a2, the condition a2=1 holds; for a3, the number a3−2=1 exists in the array.
You are given a positive integer s. Find the minimum possible size of a beautiful array with the sum of elements equal to s.

Input
The first line contains one integer t (1≤t≤5000) — the number of test cases.

Then t lines follow, the i-th line contains one integer s (1≤s≤5000) for the i-th test case.

Output
Print t integers, the i-th integer should be the answer for the i-th testcase: the minimum possible size of a beautiful array with the sum of elements equal to s.

Example
input
4
1
8
7
42
output
1
3
3
7
Note
Consider the example test:

in the first test case, the array [1] meets all conditions;
in the second test case, the array [3,4,1] meets all conditions;
in the third test case, the array [1,2,4] meets all conditions;
in the fourth test case, the array [1,4,6,8,10,2,11] meets all conditions.

一道。。。简单的构造序列题。。我竟然用回溯法搞了半天还超时。。。。菜到家了

题目意思:
给你一个n,要你求一个最短的序列和等于n,里面的元素要满足ai = 1 或 ai - 1,ai - 2的数已经在序列里面。。

可以构造成 1+3+5+7+…+2d-1 = n^2,通过这个我们知道至少要根号s个数,那么令根号s=d(取上整),则1+3+5+…+2d-3 = (d-1)的平方,而且1 <= s - (d-1)的平方 <= 2d-1, 通过这个式子就可以找到最小的序列数,(这里的s应该指的是你输入的数,加个根号取上整就是最小序列数,因此我们可以反过来,当遇到一个数的平方大于s时那么这个数就是要求的结果)

AC代码:

#include <iostream>
using namespace std;



int main(){
	
	int t;
	cin >> t;
	
	while(t--){
		
		int n;
		cin >> n;
		int ans = 1;
		
		while(ans*ans < n){
			
			ans+=1;
		}
		
		cout << ans << endl;
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值