CF1637E.Best Pair
提高+/省选-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
You are given an array a of length n. Let cntx be the number of elements from the array which are equal to x. Let's also define f(x,y) as (cntx+cnty)⋅(x+y).
Also you are given m bad pairs (xi,yi). Note that if (x,y) is a bad pair, then (y,x) is also bad.
Your task is to find the maximum value of f(u,v) over all pairs (u,v), such that u=v, that this pair is not bad, and also that u and v each occur in the array a. It is guaranteed that such a pair exists.
给你一个长度为 n 的数组 a。令 cntx 表示数组中等于 x 的元素个数。再定义函数 f(x,y)=(cntx+cnty)⋅(x+y)。
此外,你还会得到 m 个“坏”数对 (xi,yi)。注意:若 (x,y) 是坏数对,则 (y,x) 也是坏数对。
你的任务是:在所有满足以下条件的数对 (u,v) 中,找出 f(u,v) 的最大值:
- u=v;
- (u,v) 不是坏数对;
- u 和 v 均在数组 a 中出现过。
题目保证至少存在一个满足条件的数对。
输入格式
The first line contains a single integer t (1≤t≤10000) — the number of test cases.
The first line of each test case contains two integers n and m (2≤n≤3⋅105, 0≤m≤3⋅105) — the length of the array and the number of bad pairs.
The second line of each test case contains n integers a1,a2,…,an (1≤ai≤109) — elements of the array.
The i-th of the next m lines contains two integers xi and yi (1≤xi<yi≤109), which represent a bad pair. It is guaranteed that no bad pair occurs twice in the input. It is also guaranteed that cntxi>0 and cntyi>0.
It is guaranteed that for each test case there is a pair of integers (u,v), u=v, that is not bad, and such that both of these numbers occur in a.
It is guaranteed that the total sum of n and the total sum of m don't exceed 3⋅105.
第一行包含一个整数 t(1≤t≤10000)—— 测试用例的数量。
每个测试用例的第一行包含两个整数 n 和 m(2≤n≤3⋅105,0≤m≤3⋅105)—— 数组的长度和坏对的数量。
每个测试用例的第二行包含 n 个整数 a1,a2,…,an(1≤ai≤109)—— 数组的元素。
接下来的 m 行中,第 i 行包含两个整数 xi 和 yi(1≤xi<yi≤109),表示一个坏对。保证输入中不会重复出现同一个坏对。同时保证 cntxi>0 且 cntyi>0。
保证对于每个测试用例,均存在一对整数 (u,v)(其中 u=v),该对不是坏对,且 u 和 v 均在数组 a 中出现。
保证所有测试用例的 n 之和以及所有测试用例的 m 之和均不超过 3⋅105。
输出格式
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 3, 6, 7 occur in the array.
- f(3,6)=(cnt3+cnt6)⋅(3+6)=(3+2)⋅(3+6)=45. But (3,6) is bad so we ignore it.
- f(3,7)=(cnt3+cnt7)⋅(3+7)=(3+1)⋅(3+7)=40.
- f(6,7)=(cnt6+cnt7)⋅(6+7)=(2+1)⋅(6+7)=39.
The answer to the problem is max(40,39)=40.
在第一个测试用例中,数组中出现的数为 3、6、7。
- f(3,6)=(cnt3+cnt6)⋅(3+6)=(3+2)⋅(3+6)=45。但 (3,6) 是坏对,因此忽略它。
- f(3,7)=(cnt3+cnt7)⋅(3+7)=(3+1)⋅(3+7)=40。
- f(6,7)=(cnt6+cnt7)⋅(6+7)=(2+1)⋅(6+7)=39。
该问题的答案为 max(40,39)=40。
输入解题思路,AI测评打分。不知道怎么写?