CF1637E.Best Pair

提高+/省选-

通过率:0%

时间限制:2.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array aa of length nn. Let cntxcnt_x be the number of elements from the array which are equal to xx. Let's also define f(x,y)f(x, y) as (cntx+cnty)⋅(x+y)(cnt_x + cnt_y) \cdot (x + y).

Also you are given mm bad pairs (xi,yi)(x_i, y_i). Note that if (x,y)(x, y) is a bad pair, then (y,x)(y, x) is also bad.

Your task is to find the maximum value of f(u,v)f(u, v) over all pairs (u,v)(u, v), such that u≠vu \neq v, that this pair is not bad, and also that uu and vv each occur in the array aa. It is guaranteed that such a pair exists.

给你一个长度为 nn 的数组 aa。令 cntxcnt_x 表示数组中等于 xx 的元素个数。再定义函数 f(x,y)=(cntx+cnty)⋅(x+y)f(x, y) = (cnt_x + cnt_y) \cdot (x + y)。

此外,你还会得到 mm 个“坏”数对 (xi,yi)(x_i, y_i)。注意:若 (x,y)(x, y) 是坏数对,则 (y,x)(y, x) 也是坏数对。

你的任务是:在所有满足以下条件的数对 (u,v)(u, v) 中,找出 f(u,v)f(u, v) 的最大值:

  • u≠vu \neq v;
  • (u,v)(u, v) 不是坏数对;
  • uu 和 vv 均在数组 aa 中出现过。

题目保证至少存在一个满足条件的数对。

输入格式

The first line contains a single integer tt (1≤t≤10 0001 \le t \le 10\,000) — the number of test cases.

The first line of each test case contains two integers nn and mm (2≤n≤3⋅1052 \le n \le 3 \cdot 10^5, 0≤m≤3⋅1050 \le m \le 3 \cdot 10^5) — the length of the array and the number of bad pairs.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \ldots, a_n (1≤ai≤1091 \le a_i \le 10^9) — elements of the array.

The ii-th of the next mm lines contains two integers xix_i and yiy_i (1≤xi<yi≤1091 \le x_i \lt y_i \le 10^9), which represent a bad pair. It is guaranteed that no bad pair occurs twice in the input. It is also guaranteed that cntxi>0cnt_{x_i} \gt 0 and cntyi>0cnt_{y_i} \gt 0.

It is guaranteed that for each test case there is a pair of integers (u,v)(u, v), u≠vu \ne v, that is not bad, and such that both of these numbers occur in aa.

It is guaranteed that the total sum of nn and the total sum of mm don't exceed 3⋅1053 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤10 0001 \le t \le 10\,000)—— 测试用例的数量。

每个测试用例的第一行包含两个整数 nn 和 mm(2≤n≤3⋅1052 \le n \le 3 \cdot 10^5,0≤m≤3⋅1050 \le m \le 3 \cdot 10^5)—— 数组的长度和坏对的数量。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \ldots, a_n(1≤ai≤1091 \le a_i \le 10^9)—— 数组的元素。

接下来的 mm 行中,第 ii 行包含两个整数 xix_i 和 yiy_i(1≤xi<yi≤1091 \le x_i \lt y_i \le 10^9),表示一个坏对。保证输入中不会重复出现同一个坏对。同时保证 cntxi>0cnt_{x_i} \gt 0 且 cntyi>0cnt_{y_i} \gt 0。

保证对于每个测试用例,均存在一对整数 (u,v)(u, v)(其中 u≠vu \ne v),该对不是坏对,且 uu 和 vv 均在数组 aa 中出现。

保证所有测试用例的 nn 之和以及所有测试用例的 mm 之和均不超过 3⋅1053 \cdot 10^5。

输出格式

For each test case print a single integer — the answer to the problem.

对于每个测试用例,输出一个整数——即该问题的答案。

输入输出样例

  • 输入#1

    3
    6 1
    6 3 6 7 3 3
    3 6
    2 0
    3 4
    7 4
    1 2 2 3 1 5 1
    1 5
    3 5
    1 3
    2 5

    输出#1

    40
    14
    15

说明/提示

In the first test case 33, 66, 77 occur in the array.

  • f(3,6)=(cnt3+cnt6)⋅(3+6)=(3+2)⋅(3+6)=45f(3, 6) = (cnt_3 + cnt_6) \cdot (3 + 6) = (3 + 2) \cdot (3 + 6) = 45. But (3,6)(3, 6) is bad so we ignore it.
  • f(3,7)=(cnt3+cnt7)⋅(3+7)=(3+1)⋅(3+7)=40f(3, 7) = (cnt_3 + cnt_7) \cdot (3 + 7) = (3 + 1) \cdot (3 + 7) = 40.
  • f(6,7)=(cnt6+cnt7)⋅(6+7)=(2+1)⋅(6+7)=39f(6, 7) = (cnt_6 + cnt_7) \cdot (6 + 7) = (2 + 1) \cdot (6 + 7) = 39.

The answer to the problem is max⁡(40,39)=40\max(40, 39) = 40.

在第一个测试用例中,数组中出现的数为 33、66、77。

  • f(3,6)=(cnt3+cnt6)⋅(3+6)=(3+2)⋅(3+6)=45f(3, 6) = (cnt_3 + cnt_6) \cdot (3 + 6) = (3 + 2) \cdot (3 + 6) = 45。但 (3,6)(3, 6) 是坏对,因此忽略它。
  • f(3,7)=(cnt3+cnt7)⋅(3+7)=(3+1)⋅(3+7)=40f(3, 7) = (cnt_3 + cnt_7) \cdot (3 + 7) = (3 + 1) \cdot (3 + 7) = 40。
  • f(6,7)=(cnt6+cnt7)⋅(6+7)=(2+1)⋅(6+7)=39f(6, 7) = (cnt_6 + cnt_7) \cdot (6 + 7) = (2 + 1) \cdot (6 + 7) = 39。

该问题的答案为 max⁡(40,39)=40\max(40, 39) = 40。

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

首页