#P9893. [ICPC2018 Qingdao R] Soldier Game

    ID: 9278 Type: RemoteJudge 4000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>2018线段树O2优化ICPC青岛

[ICPC2018 Qingdao R] Soldier Game

题目描述

DreamGrid and BaoBao are playing a game. There are nn soldiers in the game, numbered from 11 to nn. The ii-th soldier has a power value of aia_i. DreamGrid and BaoBao are going to divide the soldiers into several teams according to the rules below:

  • A team must consist of 1 or 2 soldiers.
  • Every soldier must belong to exactly 1 team.
  • If a team consists of two soldiers (let's say they are the ii-th and the jj-th soldier), there must be ij=1|i - j| = 1.

The power value of a team is defined as the sum of the team members' power values. For the sake of fairness, they want to minimize the difference between the maximum team power value and the minimum team power value after the division. You are asked to find the minimum difference.

输入格式

There are multiple test cases. The first line of the input contains an integer TT, indicating the number of test cases. For each test case:

The first line contains an integer nn (1n1051 \le n \le 10^5), indicating the number of soldiers.

The second line contains nn integers a1,a2,...,ana_1, a_2, ... , a_n (109ai109-10^9 \le a_i \le 10^9), where aia_i indicates the power value of the ii-th soldier.

It's guaranteed that the sum of nn in all test cases will not exceed 10610^6.

输出格式

For each test case output one line containing one integer, indicating the minimum difference between the maximum team power value and the minimum team power value.

题目大意

DreamGridBaoBao 在玩游戏。游戏中有 nn 个士兵,编号为 1n1\sim n。第 ii 名士兵的权利值为 aia_iDreamGridBaoBao 将按照以下规则将士兵分成几个小组:

  • 一个队伍必须由 1122 名士兵组成。

  • 每个士兵必须只属于一个队伍。

  • 如果一个团队由两名士兵 i,ji,j 组成,则必须有 ij=1|i - j| = 1

团队的权力值被定义为队内士兵权力值的总和。

为了公平起见,他们希望最小化分区后的最大团队力量值最小团队力量值之间的差值。你被要求找出最小的差值。

有多测。

1n1051\leq n\leq 10 ^ 5ai109|a_i|\leq 10 ^ 9n106\sum n\leq 10 ^ 6

3
5
-1 4 2 1 1
4
1 3 2 4
1
7
1
2
0

提示

We now explain the first sample test case. All possible divisions are listed below.

Division Difference Division Difference
[-1], [4], [2], [1], [1] 4 - (-1) = 5 [-1, 4], [2], [1], [1] 3 - 1 = 2
[-1], [4], [2], [1, 1] [-1], [4, 2], [1, 1] 6 - (-1) = 7
[-1], [4], [2, 1], [1] [-1, 4], [2], [1, 1] 3 - 2 = 1
[-1], [4, 2], [1], [1] 6 - (-1) = 7 [-1, 4], [2, 1], [1] 3 - 1 = 2

So the answer is min(5,5,5,7,2,7,1,2)=1\min(5, 5, 5, 7, 2, 7, 1, 2) = 1.