Codeforces Round #674 (Div. 3)C. Increase and Copy

题目:
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;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值