Educational Codeforces Round 140——B. Block Towers

244 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第30天,点击查看活动详情

Educational Codeforces Round 140——B. Block Towers

Problem - B - Codeforces

There are nn block towers, numbered from 11 to nn. The ii-th tower consists of aiai blocks.

In one move, you can move one block from tower ii to tower jj, but only if ai>ajai>aj. That move increases ajaj by 11 and decreases aiai by 11. You can perform as many moves as you would like (possibly, zero).

What's the largest amount of blocks you can have on the tower 11 after the moves?

Input

The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of testcases.

The first line of each testcase contains a single integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the number of towers.

The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the number of blocks on each tower.

The sum of nn over all testcases doesn't exceed 2⋅1052⋅105.

Output

For each testcase, print the largest amount of blocks you can have on the tower 11 after you make any number of moves (possibly, zero).

Example

input

4
3
1 2 3
3
1 2 2
2
1 1000000000
10
3 8 6 7 4 1 2 4 10 1

output

3
2
500000001
9

问题解析

题目是说给你n堆砖头,你每次可以选一堆数量多于第一堆的砖头堆,把一个砖头移去第一堆砖。问第一堆砖最后最多可能有几个砖。

贪心做法,很明显的,我们每次从多于第一堆的最小的那一堆移去给第一堆。这样能给第一堆的砖肯定就是最多的。

我们对除了第一堆以外的砖进行排序。然后我们从不小于第一堆的aj开始将砖头移去给a1。

每次a1变成(aj+a1+1)/2.

AC代码

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<math.h>
#include<set>
#include <random>
#include<numeric>
#include<string>
#include<string.h>
#include<iterator>
#include<fstream>
#include<map>
#include<unordered_map>
#include<stack>
#include<list>
#include<queue>
#include<iomanip>
#include<bitset>//#pragma GCC optimize(2)
//#pragma GCC optimize(3)#define endl '\n'
//#define int ll
#define PI acos(-1)
#define INF 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PII;
const int N = 2e5 + 50, MOD = 998244353;
​
void solve()
{
    int n, x;
    cin >> n >> x;
    vector<int > v(n - 1);
    for (int i = 0; i < n - 1; i++)
    {
        cin >> v[i];
    }
    sort(v.begin(), v.end());
    for (auto& i : v)
    {
        if (i > x)
        {
            x = (i + x + 1) / 2;
        }
    }
    cout << x << endl;
}
​
signed main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}