CF1698C.3SUM Closure

普及-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

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

题目描述

You are given an array aa of length nn. The array is called 3SUM-closed if for all distinct indices ii, jj, kk, the sum ai+aj+aka_i + a_j + a_k is an element of the array. More formally, aa is 3SUM-closed if for all integers 1≤i<j<k≤n1 \leq i \lt j \lt k \leq n, there exists some integer 1≤l≤n1 \leq l \leq n such that ai+aj+ak=ala_i + a_j + a_k = a_l.

Determine if aa is 3SUM-closed.

给你一个长度为 nn 的数组 aa。如果对所有互不相同的下标 ii、jj、kk,其和 ai+aj+aka_i + a_j + a_k 均属于该数组,则称该数组是 3SUM-封闭的(3SUM-closed)。更准确地说,若对所有满足 1≤i<j<k≤n1 \leq i \lt j \lt k \leq n 的整数 i,j,ki,j,k,均存在某个整数 1≤l≤n1 \leq l \leq n,使得 ai+aj+ak=ala_i + a_j + a_k = a_l,则称 aa 是 3SUM-封闭的。

请判断数组 aa 是否为 3SUM-封闭的。

输入格式

The first line contains an integer tt (1≤t≤10001 \leq t \leq 1000) — the number of test cases.

The first line of each test case contains an integer nn (3≤n≤2⋅1053 \leq n \leq 2 \cdot 10^5) — the length of the array.

The second line of each test case contains nn integers a1,a2,…,ana_1, a_2, \dots, a_n (−109≤ai≤109-10^9 \leq a_i \leq 10^9) — the elements of the array.

It is guaranteed that the sum of nn across all test cases does not exceed 2⋅1052 \cdot 10^5.

第一行包含一个整数 tt(1≤t≤10001 \leq t \leq 1000),表示测试用例的数量。

每个测试用例的第一行包含一个整数 nn(3≤n≤2⋅1053 \leq n \leq 2 \cdot 10^5),表示数组的长度。

每个测试用例的第二行包含 nn 个整数 a1,a2,…,ana_1, a_2, \dots, a_n(−109≤ai≤109-10^9 \leq a_i \leq 10^9),表示数组的元素。

保证所有测试用例的 nn 之和不超过 2⋅1052 \cdot 10^5。

输出格式

For each test case, output "YES" (without quotes) if aa is 3SUM-closed and "NO" (without quotes) otherwise.

You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).

对于每个测试用例,如果 aa 是 3SUM-闭的,则输出 "YES"(不带引号);否则输出 "NO"(不带引号)。

你可以以任意大小写形式输出 "YES" 和 "NO"(例如,字符串 "yEs"、"yes" 和 "Yes" 均会被识别为肯定回答)。

输入输出样例

  • 输入#1

    4
    3
    -1 0 1
    5
    1 -2 -2 1 -3
    6
    0 0 0 0 0 0
    4
    -1 2 -3 4

    输出#1

    YES
    NO
    YES
    NO

说明/提示

In the first test case, there is only one triple where i=1i=1, j=2j=2, k=3k=3. In this case, a1+a2+a3=0a_1 + a_2 + a_3 = 0, which is an element of the array (a2=0a_2 = 0), so the array is 3SUM-closed.

In the second test case, a1+a4+a5=−1a_1 + a_4 + a_5 = -1, which is not an element of the array. Therefore, the array is not 3SUM-closed.

In the third test case, ai+aj+ak=0a_i + a_j + a_k = 0 for all distinct ii, jj, kk, and 00 is an element of the array, so the array is 3SUM-closed.

在第一个测试用例中,仅存在一个三元组,其中 i=1i=1、j=2j=2、k=3k=3。此时,a1+a2+a3=0a_1 + a_2 + a_3 = 0,而 00 是数组中的一个元素(即 a2=0a_2 = 0),因此该数组是 3SUM-闭的。

在第二个测试用例中,a1+a4+a5=−1a_1 + a_4 + a_5 = -1,而 −1-1 不是数组中的元素。因此,该数组不是 3SUM-闭的。

在第三个测试用例中,对所有互不相同的 ii、jj、kk,均有 ai+aj+ak=0a_i + a_j + a_k = 0,且 00 是数组中的一个元素,因此该数组是 3SUM-闭的。

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

首页