题目:
C. Increase and Copy
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
Initially, you have the array aa consisting of one element 11 (a=[1]a=[1]).
In one move, you can do one of the following things:
Increase some (single) element of aa by 11 (choose some ii from 11 to the current length of aa and increase aiai by one);
Append the copy of some (single) element of aa to the end of the array (choose some ii from 11 to the current length of aa and append aiai to the end of the array).
For example, consider the sequence of five moves:
You take the first element a1a1, append its copy to the end of the array and get a=[1,1]a=[1,1].
You take the first element a1a1, increase it by 11 and get a=[2,1]a=[2,1].
You take the second element a2a2, append its copy to the end of the array and get a=[2,1,1]a=[2,1,1].
You take the first element a1a1, append its copy to the end of the array and get a=[2,1,1,2]a=[2,1,1,2].
You take the fourth element a4a4, increase it by 11 and get a=[2,1,1,3]a=[2,1,1,3].
Your task is to find the minimum number of moves required to obtain the array with the sum at least nn.
You have to answer tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤10001≤t≤1000) — the number of test cases. Then tt test cases follow.
The only line of the test case contains one integer nn (1≤n≤1091≤n≤109) — the lower bound on the sum of the array.
Output
For each test case, print the answer: the minimum number of moves required to obtain the array with the sum at least nn.
Example
input
5
1
5
42
1337
1000000000
output
0
3
11
72
63244
题意:每次可进行两种操作,一种是将一个元素加一,另一种是将某个元素复制后加入数组(也就是2),求数组和为n的最小操作次数。
分析:这题很明显求最优解问题,数据范围不大,可以暴力用贪心算法进行计算,题目难度偏低,
可以先加一再2,也可以先全部加一,加到sqrt(n)附近,再*2。
ac代码:
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t,n;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
int ans=1e9;
int t=sqrt(n)-1;
for(int j=0;j<=t;j++)
{
int now=j+1;
int num=(n-1)/now;
ans=min(ans,j+num);
}
printf("%d\n",ans);
}
return 0;
}