Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解

Posted Tisfy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解相关的知识,希望对你有一定的参考价值。

Educational Codeforces Round 111 (Rated for Div. 2)-A. Find The Array

传送门
Time Limit: 1 second
Memory Limit: 256 megabytes

Problem Description

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

For example:

You are given a positive integer s s s. Find the minimum possible size of a beautiful array with the sum of elements equal to s s s.

Input

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

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

Output

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

Sample Input

4
1
8
7
42

Sample Onput

1
3
3
7

Note

Consider the example test:

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

题目大意

一个漂亮数组满足:这个元素t是1 或者t-1或t-2在这个数组中。
给你一个数,让你找一个漂亮数组使得数组的和是这个数,问漂亮数组长度最少是多少。

解题思路

两个定理:

  • 长度为n的漂亮数组的和的最大值是 1 + 3 + 5 + ⋯ + 2 ∗ n − 1 1+3+5+\\cdots+2*n-1 1+3+5++2n1
  • 小于上述值的数可由n长度不大于n的漂亮数组表示。

比如长度为3的漂亮数组的最大值是 1 + 3 + 5 = 9 1+3+5=9 1+3+5=9,那么8可表示成 1 + 3 + 4 1+3+4 1+3+4是长度为3的漂亮数组。


AC代码

#include <bits/stdc++.h>
using namespace std;
#define mem(a) memset(a, 0, sizeof(a))
#define dbg(x) cout << #x << " = " << x << endl
#define fi(i, l, r) for (int i = l; i < r; i++)
#define cd(a) scanf("%d", &a)
typedef long long ll;
int main()
{
    int N;
    cin>>N;
    while(N--)
    {
        int n;
        cin>>n;
        int s=0; // 数组的和
        int t=0; // 几个元素
        for(int i=1;i<=50000;i+=2) // 1+3+5+... 其实上限不用到50000就break出了
        {
            t++; // 元素个数+1
            s+=i; // 和+i
            if(s>=n) // 和大于等于n
            {
                cout<<t<<endl; //用t个能表示
                break;
            }
        }
    }
    return 0;
}

原创不易,转载请附上原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/118771980

以上是关于Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解的主要内容,如果未能解决你的问题,请参考以下文章

Codeforces Round #717 (Div. 2)-A. Tit for Tat-题解

区间倍增dpD. Cut——Codeforces Round #717 (Div. 2)

Codeforces Round #717 (Div. 2)-C. Baby Ehab Partitions Again-题解

Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解

Codeforces Round #717 (Div. 2)-B. Maximum Cost Deletion-题解

Codeforces Round #717 (Div. 2)-C. Baby Ehab Partitions Again-题解