CF2056D.Unique Median

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:512MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

An array bb of mm integers is called good if, when it is sorted, b⌊m+12⌋=b⌈m+12⌉b_{\left\lfloor \frac{m + 1}{2} \right\rfloor} = b_{\left\lceil \frac{m + 1}{2} \right\rceil}. In other words, bb is good if both of its medians are equal. In particular, ⌊m+12⌋=⌈m+12⌉\left\lfloor \frac{m + 1}{2} \right\rfloor = \left\lceil \frac{m + 1}{2} \right\rceil when mm is odd, so bb is guaranteed to be good if it has an odd length.

You are given an array aa of nn integers. Calculate the number of good subarrays∗^{\text{∗}} in aa.

∗^{\text{∗}}An array xx is a subarray of an array yy if xx can be obtained from yy by the deletion of several (possibly, zero or all) elements from the beginning and several (possibly, zero or all) elements from the end.

一个包含 mm 个整数的数组 bb 被称为好数组,当且仅当将其排序后满足

b⌊m+12⌋=b⌈m+12⌉.b_{\left\lfloor \frac{m + 1}{2} \right\rfloor} = b_{\left\lceil \frac{m + 1}{2} \right\rceil}.

换言之,bb 是好数组当且仅当它的两个中位数相等。特别地,当 mm 为奇数时,恒有

⌊m+12⌋=⌈m+12⌉,\left\lfloor \frac{m + 1}{2} \right\rfloor = \left\lceil \frac{m + 1}{2} \right\rceil,

因此所有长度为奇数的数组必定是好数组。

给定一个包含 nn 个整数的数组 aa,请计算 aa 中好子数组∗^{\text{∗}} 的数量。

∗^{\text{∗}} 数组 xx 是数组 yy 的子数组,当且仅当 xx 可通过从 yy 的开头删除若干(可能为零个或全部)元素、并从 yy 的末尾删除若干(可能为零个或全部)元素而得到。

输入格式

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

The first line of each test case contains a single integer nn (1≤n≤1051 \le n \le 10^5) — the length of the array.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤101 \le a_i \le \color{red}{\textbf{10}}) — the given array.

It is guaranteed that the sum of nn over all test cases does not exceed 10510^5.

每个测试包含多个测试用例。第一行包含测试用例的数量 tt(1≤t≤1041 \le t \le 10^4)。随后是各测试用例的描述。

每个测试用例的第一行包含一个整数 nn(1≤n≤1051 \le n \le 10^5)—— 数组的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤101 \le a_i \le \color{red}{\textbf{10}})—— 给定的数组。

保证所有测试用例的 nn 之和不超过 10510^5。

输出格式

For each test case, output a single integer representing the number of good subarrays in aa.

对于每个测试用例,输出一个整数,表示数组 aa 中“好”子数组的个数。

输入输出样例

  • 输入#1

    3
    4
    1 1 1 1
    5
    1 10 2 3 3
    10
    6 3 2 3 5 3 4 2 3 5

    输出#1

    10
    11
    42

说明/提示

In the first case, every subarray is good since all its elements are equal to 11.

In the second case, an example of a good subarray is b=[10,2,3,3]b = [10, 2, 3, 3]. When it is sorted, b=[2,3,3,10]b = [2, 3, 3, 10], so b⌊4+12⌋=b⌈4+12⌉=b2=b3=3b_{\left\lfloor \frac{4 + 1}{2} \right\rfloor} = b_{\left\lceil \frac{4 + 1}{2} \right\rceil} = b_2 = b_3 = 3. Another example would be b=[1,10,2]b = [1, 10, 2]. On the other hand, b=[1,10]b = [1, 10] is not good as its two medians are 11 and 1010, which are not equal.

第一种情况中,每个子数组都是“好”的,因为其所有元素都等于 11。

第二种情况中,一个“好”子数组的例子是 b=[10,2,3,3]b = [10, 2, 3, 3]。将其排序后得到 b=[2,3,3,10]b = [2, 3, 3, 10],因此 b⌊4+12⌋=b⌈4+12⌉=b2=b3=3b_{\left\lfloor \frac{4 + 1}{2} \right\rfloor} = b_{\left\lceil \frac{4 + 1}{2} \right\rceil} = b_2 = b_3 = 3。另一个例子是 b=[1,10,2]b = [1, 10, 2]。而 b=[1,10]b = [1, 10] 则不是“好”子数组,因为它的两个中位数为 11 和 1010,二者不相等。

输入解题思路,AI测评打分。不知道怎么写?

首页