首页 > 其他分享 >D. Lonely Mountain Dungeons

D. Lonely Mountain Dungeons

时间:2024-02-13 20:44:56浏览次数:19  
标签:Mountain Lonely army cdot Dungeons second squad sent first

D. Lonely Mountain Dungeons

Once, the people, elves, dwarves, and other inhabitants of Middle-earth gathered to reclaim the treasures stolen from them by Smaug. In the name of this great goal, they rallied around the powerful elf Timothy and began to plan the overthrow of the ruler of the Lonely Mountain.

The army of Middle-earth inhabitants will consist of several squads. It is known that each pair of creatures of the same race, which are in different squads, adds $b$ units to the total strength of the army. But since it will be difficult for Timothy to lead an army consisting of a large number of squads, the total strength of an army consisting of $k$ squads is reduced by $(k - 1) \cdot x$ units. Note that the army always consists of at least one squad.

It is known that there are $n$ races in Middle-earth, and the number of creatures of the $i$-th race is equal to $c_i$. Help the inhabitants of Middle-earth determine the maximum strength of the army they can assemble.

Input

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 2 \cdot 10^4$) — the number of test cases. The description of the test cases follows.

The first line of each test case contains three integers $n$, $b$, and $x$ ($1 \le n \le 2 \cdot 10^5$, $1 \le b \le 10^6, 0 \le x \le 10^9$) — the number of races and the constants $b$ and $x$ described above.

The second line of each test case contains $n$ integers $c_1, c_2, \ldots, c_n$ ($1 \le c_i \le 2 \cdot 10^5$) — the number of creatures of each of the $n$ races.

It is guaranteed that the sum of the values $c_1 + c_2 + \ldots + c_n$ over all test cases does not exceed $2 \cdot 10^5$.

Output

For each test case, output a single integer — the maximum strength of the army that the inhabitants of Middle-earth can assemble.

Example

Input

5
3 1 0
1 2 3
3 5 10
2 5 3
4 3 3
3 2 1 2
4 1 0
4 1 4 2
4 1 10
4 1 4 2

Output

4
40
9
13
0

Note

In the first test case, the inhabitants of Middle-earth can form $3$ squads. Since $x = 0$, the army's strength will not decrease due to the number of squads. The inhabitants can be distributed among the squads as follows:

  • The single representative of the first species can be sent to the first squad.
  • The first representative of the second species can be sent to the first squad, the second representative of the second species can be sent to the second squad. Then the total strength of the army will increase by $b = 1$.
  • The first representative of the third species can be sent to the first squad, the second representative of the third species can be sent to the second squad, the third representative of the third species can be sent to the third squad. Then the total strength of the army will increase by $3 \cdot b = 3$, as they form three pairs in different squads.

Thus, the total strength of the army is $4$.

In the second test case, the inhabitants of Middle-earth can form $3$ squads. Since $x = 10$, the army's strength will decrease by $20$. The inhabitants can be distributed among the squads as follows:

  • The first representative of the first species can be sent to the first squad, the second representative of the first species can be sent to the second squad. Then the total strength of the army will increase by $b = 5$.
  • The first and second representatives of the second species can be sent to the first squad, the third and fourth representatives of the second species can be sent to the second squad, the fifth representative of the second species can be sent to the third squad. Then the total strength of the army will increase by $8 \cdot b = 40$.
  • The first representative of the third species can be sent to the first squad, the second representative of the third species can be sent to the second squad, the third representative of the third species can be sent to the third squad. Then the total strength of the army will increase by $3 \cdot b = 15$, as they form three pairs in different squads.

Thus, the total strength of the army is $5 + 40 + 15 - 20 = 40$.

 

解题思路

  对于固定的 $k$,容易知道每个 $c_i$ 的分配是相互独立的,因此可以依次单独考虑。当尽可能把 $c_i$ 平均分配成 $k$ 份时,能获得的数对数量最大。即有 $r = c_i \bmod k$ 条队被分配了 $\left\lceil \frac{c}{k} \right\rceil$ 个,剩余的 $k-r$ 条队被分配了 $\left\lfloor \frac{c}{k} \right\rfloor$ 个。数对有三种情况:两个都来自被分配了 $\left\lfloor \frac{c}{k} \right\rfloor$ 的队,数量为 $C_{k-r}^{2} \cdot \left\lfloor \frac{c}{k} \right\rfloor^2$。两个都来自被分配了 $\left\lceil \frac{c}{k} \right\rceil$ 的队,数量为 $C_{r}^{2} \cdot \left\lceil \frac{c}{k} \right\rceil^2$。两个来自被分配了不同个数的队,数量为 $(k-r) \cdot r \cdot \left\lfloor \frac{c}{k} \right\rfloor \cdot \left\lceil \frac{c}{k} \right\rceil$。

  对于每个 $c_i$,枚举 $j \in [1, c_i]$ 用上面的方法来计算当 $k = j$ 时 $c_i$ 的贡献(数对)是多少,并累加到 $s_j$ 中。

  记 $m = \max\limits_{1 \leq i \leq n}\{c_i\}$,枚举 $1 \sim m$ 表示 $k$ 所有可能的情况,并取最大值。其中对于 $c_i < k$ 的情况,显然每条队只分配 $1$ 个是最优的,数对数量为 $C_{c_i}^{2}$。用 $\text{sum}$ 来维护所有 $c_i < k$ 的 $C_{c_i}^{2}$ 的总和。那么当有 $k$ 条队时,总的数对数量就是 $s_k + \text{sum}$,对答案的贡献为 $(s_k + \text{sum}) \cdot b + (k-1) \cdot x$。

  AC 代码如下,时间复杂度为 $O\left(\sum{c_i}\right)$:

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2e5 + 10;

int c[N], cnt[N];
LL s[N];

LL get(int c, int k) {
    int r = c % k;
    LL ret = 0;
    if (k - r >= 2) ret += (k - r) * (k - r - 1ll) / 2 * (c / k) * (c / k);
    if (r >= 2) ret += r * (r - 1ll) / 2 * ((c + k - 1) / k) * ((c + k - 1) / k);
    ret += 1ll * (k - r) * r * (c / k) * ((c + k - 1) / k);
    return ret;
}

void solve() {
    int n, b, x;
    scanf("%d %d %d", &n, &b, &x);
    for (int i = 0; i < n; i++) {
        scanf("%d", c + i);
        cnt[c[i]]++;
    }
    for (int i = 0; i < n; i++) {
        for (int j = 1; j <= c[i]; j++) {
            s[j] += get(c[i], j);
        }
    }
    LL ret = 0, sum = 0;
    int m = *max_element(c, c + n);
    for (int i = 1; i <= m; i++) {
        ret = max(ret, (s[i] + sum) * b - (i - 1ll) * x);
        sum += get(i, i) * cnt[i];
    }
    printf("%lld\n", ret);
    for (int i = 0; i < n; i++) {
        cnt[c[i]] = 0;
    }
    for (int i = 1; i <= m; i++) {
        s[i] = 0;
    }
}

int main() {
    int t;
    scanf("%d", &t);
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round 924 Editorial:https://codeforces.com/blog/entry/125740

标签:Mountain,Lonely,army,cdot,Dungeons,second,squad,sent,first
From: https://www.cnblogs.com/onlyblues/p/18014805

相关文章

  • CF1928D Lonely Mountain Dungeons
    原题链接设\(F(n,m)\)是将\(n\)个同种族的人放到\(m\)个队伍中可以获得的贡献。可以发现在同一个队伍中的人不能互相产生贡献,所以尽可能平均分配是最优的。设\(p=\lfloor\dfrac{n}{m}\rfloor,q=n\bmodm\),那么有\(m-q\)个队伍中有\(p\)个人,\(q\)个队伍中有\(p+1\)......
  • P1561 [USACO12JAN] Mountain Climbing S
    P1561[USACO12JAN]MountainClimbingS贪心思路首先我们设\(c_i\)为第\(i\)头牛上山后又下山的时间。那么有两种情况,我们分类讨论。第\(i\)头牛上到山顶时,第\(i-1\)头牛还未下到山脚。第\(i-1\)头牛下山完毕但第\(i\)头牛还在上山。那么\(c_i\)的公式......
  • CF1654G Snowy Mountain 题解
    题目链接点击打开链接题目解法很牛的题显然可以\(O(n)\)多源\(bfs\)求出\(h_i\)考虑从\(st\)开始最优的操作是什么?先延最短路径到\(p\),然后找到\(p\)的相邻点\(q\),满足\(h_p=h_q\),在\(p,q\)之间横跳,耗完所有动能,然后直接滑下去(不经过高度相同的点)为什么到\(p......
  • CF1322E - Median Mountain Range - 总结
    CF1322E-MedianMountainRange考虑分别对每个位置求出最后的数字。先枚举出这个数\(x\),并将\(a_i\gex\)的数设为\(1\),\(a_i<x\)的数设为\(0\),然后做题目中的操作,若为\(0\),则最终结果小于\(x\),为\(1\)则大于等于\(x\)。使用二分可以优化到\(\Omicron(n^2\log......
  • Kattis - A Complex Problem (The 2023 ICPC Rocky Mountain Regional Contest)
    IntroThiswasoneoftheproblemsIdidn'tdoduringtheregionalcontest.Oneofmyteammatessolvedit.ObservationTherearefewthingstonote.Firsttypeofnotation:subsetmeansthatA$\subset$B,buttherecanbecasesthatsubsetforms......
  • CERC2014 Mountainous landscape
    1ay1D。这是一个跑不过双\(\log\)的单\(\log\)做法。考虑双\(\log\)做法是怎么做的。令\(a_i(1\lei\len)\)为给定的\(x\)坐标递增的点序列,开一棵线段树维护区间上凸壳,第\(i\)次查询相当于在\([i+2,n]\)区间组成的点的上凸壳中,找到一个在经过\(a_i,a_{i+1}\)......
  • CF1852C Ina of the Mountain
    *2400https://codeforces.com/problemset/problem/1852/C如果没有\(\modk\)的限制的话,我们都会做,因为都是正数,那么\(\sum_i^nd_i>0\),因此,答案即为\(\sum[d_i>0]d_i\)。但是现在多了一个操作,即为区间加\(k\),那么转到差分数组就是\(d_l+k,d_r-k\),且该操作不花费。观察,差......
  • 【题解】CF1852C Ina of the Mountain
    我们先从题目的一部分入手。如果说,我们没有当一个数为\(0\)时,让这个数变成\(k\)的性质,我们如何求答案呢?很简单,在图上就是:绿色线段的长度加起来即为答案(本图中是\(6\))我们考虑很显然地,将一个数从\(0\)变为\(k\)即为将一个数一开始加上\(k\)我们如果要让第\(i\)列......
  • Codeforces Round 888 (Div. 3)G. Vlad and the Mountains(数据结构,图论)
    题目链接:https://codeforces.com/contest/1851/problem/G 大致题意: 给出n个点m条边的无向图,每个点有点权h【i】。从点i到点j会消耗h【j】-h【i】的能量,如果小于0,那么就是恢复对应绝对值的能量。 进行q次询问,每次询问包含起点s,终点t,能量e,能量在移动过程中不能小......
  • Codeforces Round 887 (Div. 1)C. Ina of the Mountain(数据结构,反悔贪心)
    题目链接:https://codeforces.com/problemset/problem/1852/C 题意: 给定一个长度为n的序列和正整数k; 每次可以选取任意一个区间,将区间内每个数减1; 如果出现一个数变成0,那么那个数变成k; 问至少操作多少次可以使得每个数变成k; 分析: 将每个数值抽象为对应高度的......