Harshith is the president of TollClub. He tasks hi...

Tạo vào: 21 tháng 6, 2025

Trả lời bằng O4 Mini High bởi Chat01

Câu hỏi

Harshith is the president of TollClub. He tasks his subordinate Aryan to oversee a toll plaza with nn lanes. Initially, the ii-th lane has aia_i cars waiting in a queue. Exactly one car from the front of each lane passes through the toll every second.

The angriness of a car is defined as the number of seconds it had to wait before passing through the toll. Consider it takes 1 sec for each car to pass the toll, i.e., the first car in a lane has angriness 11, the second car has angriness 22, and so on.

To reduce congestion and frustration, cars are allowed to switch lanes. A car can instantly move to the back of any other lane at any time. However, changing lanes increases its angriness by an additional kk units due to the confusion caused by the lane change.

Harshith, being the awesome person he is, wants to help the drivers by minimising the total angriness of all cars. He asks Aryan to do so or get fired. Aryan is allowed to change lanes of any car anytime (possibly zero), but his goal is to find the minimum possible total angriness if the lane changes are done optimally. Help Aryan retain his job by determining the minimum angriness he can achieve.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t1041 \le t \le 10^4). The description of the test cases follows.

The first line of each test case contains two integers nn and kk (1n21051 \le n \le 2 \cdot 10^5, 1k1061 \le k \le 10^6) — the number of lanes and the increment in angriness on a lane change.

The second line of each test case contains nn space-separated integers, denoting array aa — the ii-th number representing the number of cars in the ii-th lane (1ai1061 \le a_i \le 10^6).

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Note that the sum of maxai\max a_i over all test cases is not bounded.

Output

For each test case, output a single integer in a new line, denoting the minimum total angriness.

Example
InputCopy
6
3 4
13 7 4
4 9
6 12 14 5
5 3
2 4 13 5 10
1 7
6
13 4
10 26 34 39 9 43 48 41 1 38 13 4 46
16 3
176342 171863 70145 80835 160257 136105 78541 100795 114461 45482 68210 51656 29593 8750 173743 156063
OutputCopy
123
219
156
21
5315
82302351405

Note

In the first test case, Aryan shifts two cars from lane 11 to lane 33, after which the array becomes [11,7,6][11, 7, 6]. The total angriness is 11122+782+672+24=123\frac{11\cdot 12}{2}+\frac{7\cdot 8}{2}+\frac{6\cdot 7}{2}+2\cdot 4=123. It can be proven that this is the minimum possible angriness.

In the fourth test case, there is only one lane, so cars can't shift lanes. Total angriness is 672=21\frac{6\cdot7}{2}=21.中文c++回答。

Suy nghĩ

Considering optimal lane redistribution

For this problem, I need to propose a strategy that minimizes angriness across lanes. The angriness for each lane is the sum of waiting times for cars in that lane, which is given by m×(m+1)/2m \times (m+1) / 2 for m cars. If cars are moved between lanes, extra costs are incurred (k per moved car). I'll aim to minimize total angriness by redistributing cars in a way that balances the lanes, factoring in both angriness and movement costs.

Optimizing lane redistribution

When moving a car to a new lane, it goes to the end of the target lane, and its waiting time increases depending on where at the end it goes, which adds to the angriness. We need to minimize both total waiting times and movement costs (k). The problem can be seen as minimizing the sum of completion times plus costs for moving cars. The goal is to optimally redistribute cars across lanes to minimize these factors, with each lane processing one car per second.

Optimizing job redistribution across lanes

I need to minimize waiting time and cost for moving cars between lanes. Each car either stays in its original lane or is moved, with a cost for each move. To distribute jobs (cars) optimally, I have to decide how many cars from each lane stay and how many are moved, while balancing the lanes’ queues. Essentially, I must distribute these cars in a way that minimizes the total waiting time. This involves distributing extra cars across lanes, ensuring balanced queues to minimize costs.

Balancing final assignments and minimizing cost

To minimize waiting times and costs while balancing cars across lanes, I need to consider the number of cars to move and keep in each lane. The challenge is that moving cars increases the overall cost due to the additional "angriness" cost, which depends on the number of cars moved. My goals are to keep as many original cars as possible (to minimize move costs) and balance the lanes, while strategically moving cars to ensure minimal waiting times. It's a complex tradeoff between balancing the lanes and managing move-related penalties.

Minimizing waiting times and assignment cost

To minimize the total waiting time and cost for moving cars between lanes, the key is managing which cars stay in their original lane and which are moved. If we move a car, the incurred cost is a set penalty (k) plus the new waiting time at the target lane. So, if we can move cars instantly at time 0, their waiting times depend only on their final lane position. This means we focus on assigning jobs to lanes, filling each lane with as many in-place jobs as possible to minimize the movement cost.

Thought for 1m 46s

Trả lời

下面给出思路与 C++ 实现。

思路(中文)

我们可以把所有车辆在时刻 0 直接重新分配到 n 条车道:每辆车若保留在原道,则等待时间等于它在该道的新排队位置;若改道,则在新道排到队尾,等待时间加上额外的 k 的“混乱惩罚”。记各道最终分到的车辆数为 bjb_j,满足 bj=N\sum b_j=NN=aiN=\sum a_i),将改道的车辆数记为 mm。总愤怒度可写成

j=1nbj(bj+1)2  +  km.\sum_{j=1}^n\frac{b_j(b_j+1)}2 \;+\;k\cdot m.

这个问题是“可分配凸代价”的经典模型,对每个车道,第 xx 辆车的边际成本(增量愤怒度)为

gj(x)={x,xaj,x+k,x>aj.g_j(x)= \begin{cases} x, & x\le a_j,\\ x+k, & x> a_j. \end{cases}

要最小化总成本,就相当于在所有 nn 个序列 {gj(1),gj(2),}\{g_j(1),g_j(2),\dots\} 中取前 NN 小的增量之和。令阈值 λ\lambda 为第 NN 小的位置,即满足

C(λ)=j=1n#{x:gj(x)λ}  N,C(\lambda)=\sum_{j=1}^n\#\{x:g_j(x)\le\lambda\}\;\ge N,

并且对于 λ1\lambda-1 时不满足。二分 λ\lambda(它的范围在 [0,maxai+k][0,\max a_i+k] 之内)即可找到最小的满足条件的 λ\lambda。找出 λ\lambda 后,再在线性时间内累加所有小于等于 λ\lambda 的增量之和,再减去多余的 (C(λ)N)(C(\lambda)-N) 个值(均为 λ\lambda)即可得到答案。

整体复杂度 O(nlog(maxai+k))O(n\log(\max a_i+k)),满足 n2105n\le2\cdot10^5ai,k106a_i,k\le10^6 的要求。


cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using i128 = __int128_t; int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int T; cin >> T; while(T--){ int n; ll k; cin >> n >> k; vector<ll> a(n); ll N = 0, maxa = 0; for(int i = 0; i < n; i++){ cin >> a[i]; N += a[i]; maxa = max(maxa, a[i]); } // 二分 lambda ll low = 0, high = maxa + k; while(low < high){ ll mid = (low + high) >> 1; // 计算 C(mid) ll cnt = 0; for(int i = 0; i < n; i++){ if(mid <= a[i]) cnt += mid; else if(mid <= a[i] + k) cnt += a[i]; else cnt += (mid - k); if(cnt >= N) break; } if(cnt < N) low = mid + 1; else high = mid; } ll lam = low; // 计算 C(lambda) 和 S_total(lambda) ll C_lambda = 0; i128 S_total = 0; for(int i = 0; i < n; i++){ ll ai = a[i]; if(lam <= ai){ // 全部取免费部分 C_lambda += lam; S_total += (i128)lam * (lam + 1) / 2; } else if(lam <= ai + k){ // 只能取 ai 个免费 C_lambda += ai; S_total += (i128)ai * (ai + 1) / 2; } else{ // 免费 ai 个 + 付费部分 lam-k-ai 个 ll mj = lam - k - ai; // 付费部分的长度 C_lambda += (ai + mj); // 免费部分 i128 free_sum = (i128)ai * (ai + 1) / 2; // 付费部分:sum_{d=1..mj} (ai + d + k) // = sum_{x=ai+1..ai+mj} x + k*mj i128 exp_x = (i128)(2*ai + mj + 1) * mj / 2; i128 exp_sum = exp_x + (i128)k * mj; S_total += free_sum + exp_sum; } } // 多余的 C(lambda)-N 个增量均为 lam,需要减去 ll R = C_lambda - N; i128 ans128 = S_total - (i128)R * lam; // 输出 i128 ll answer = (ll)ans128; cout << answer << "\n"; } return 0; }

复杂度分析

  • 对每个测试用例,先 O(n)O(n) 扫描求和和找最大 aia_i
  • 二分搜索 λ\lambda 范围在 [0,maxai+k][0,\max a_i+k],长度至多 2×1062\times10^6,二分约 21 次,每次 O(n)O(n) 计算总计数 C(λ)C(\lambda)
  • 最后一次线性 O(n)O(n) 计算总愤怒度。

总体 O(nlog(maxai+k))O\bigl(n\log(\max a_i+k)\bigr) 足够高效。

Chia sẻ Q&A này