CF 898 div4 E. Building an Aquarium

目录

  • Problem Statement
    • Input
    • Output
    • SAMPLES
      • input
      • output
      • Note
  • 解题过程
    • 上代码
  • 总结

CF 898 div4 E. Building an Aquarium,题目大意:t组数据,每组数据第一行n,m,表示有n个水位,可填充m个水位,求最高填充多高的水位

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

Problem Statement

You love fish, that’s why you have decided to build an aquarium. You have a piece of coral made of n columns, the i-th of which is ai units tall. Afterwards, you will build a tank around the coral as follows:

  • Pick an integer h ≥1— the height of the tank. Build walls of height h on either side of the tank.
  • Then, fill the tank up with water so that the height of each column is h, unless the coral is taller than h; then no water should be added to this column.

For example, with a=[3,1,2,4,6,2,5] and a height of h=4, you will end up using a total of w=8 units of water, as shown.

You can use at most x units of water to fill up the tank, but you want to build the biggest tank possible. What is the largest value of h you can select?

CF 898 div4 E. Building an Aquarium_第1张图片

Input

The first line contains a single integer t(1≤t≤10^4 ) — the number of test cases.

The first line of each test case contains two positive integers n and x (1≤n≤2*10^5; 1≤x≤10 ^9) — the number of columns of the coral and the maximum amount of water you can use.

The second line of each test case contains n space-separated integers ai (1≤ai≤10^9) — the heights of the coral.

The sum of n over all test cases doesn’t exceed 2*10^5.

Output

For each test case, output a single positive integer h (h≥1) — the maximum height the tank can have, so you need at most x units of water to fill up the tank.

We have a proof that under these constraints, such a value of h always exists.

SAMPLES

input

5
7 9
3 1 2 4 6 2 5
3 10
1 1 1
4 1
1 4 3 4
6 1984
2 6 5 9 1 8
1 1000000000
1

output

4
4
2
335
1000000001

Note

The first test case is pictured in the statement. With h=4 we need 8 units of water, but if h is increased to 5 we need 13 units of water, which is more than x=9 . So h=4 is optimal.

In the second test case, we can pick h=4 and add 3 units to each column, using a total of 9 units of water. It can be shown that this is optimal.

In the third test case, we can pick h=2 and use all of our water, so it is optimal.

解题过程

我的解题过程是一步一步来的,首先先写出一个for循环来判断是否全部元素一样,如第二个样例输入;再考虑,还有两种情况:第一就是可使用的水位不够如第一个样例,第二是可使用的水位充足可以再次循环如最后一个样例。

上代码

#include
using namespace std;
#define int long long
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n,m;
        cin>>n>>m;
        int s[n];
        for(int i=1; i<=n; i++)
            cin>>s[i];
        sort(s+1,s+1+n);
        //for(int i=1;i<=n;i++)
        //    cout<
        int k=1;
        int ans=s[1];
        int term=0;
        int next=0;
        while(1)
        {
            int first=s[1];
            k=1;
            int flag=0;
            for(int i=2; i<=n; i++)
            {
                if(s[i]==s[i-1])
                {
                    k++;
                }
                else
                {
                    term=s[i-1];
                    next=s[i];
                    flag=1;
                    //cout<
                    break;
                }
            }
            if(flag)
            {
                if(k*(next-first)<=m)
                {
                    m-=k*(next-first);
                    for(int i=1;i<=k+1;i++)
                        s[i]=next;
                    ans=next;
                }
                else
                {
                    cout<<term+m/k<<endl;
                    break;
                }
            }
            if(k==n)
                break;
        }

        if(k==n)
        {
            ans+=m/n;
            cout<<ans<<endl;
        }



    }
}


总结

主要是解题的思路与步骤,需要一步一步根据样例来写,注意要开longlong。

你可能感兴趣的:(c++,算法,数据结构,开发语言)