• 803_Div3(3SUM Closure)


    C. 3SUM Closure

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You are given an array 𝑎a of length 𝑛n. The array is called 3SUM-closed if for all distinct indices 𝑖i, 𝑗j, 𝑘k, the sum 𝑎𝑖+𝑎𝑗+𝑎𝑘ai+aj+ak is an element of the array. More formally, 𝑎a is 3SUM-closed if for all integers 1≤𝑖<𝑗<𝑘≤𝑛1≤i<j<k≤n, there exists some integer 1≤𝑙≤𝑛1≤l≤n such that 𝑎𝑖+𝑎𝑗+𝑎𝑘=𝑎𝑙ai+aj+ak=al.

    Determine if 𝑎a is 3SUM-closed.

    Input

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

    The first line of each test case contains an integer 𝑛n (3≤𝑛≤2⋅1053≤n≤2⋅105) — the length of the array.

    The second line of each test case contains 𝑛n integers 𝑎1,𝑎2,…,𝑎𝑛a1,a2,…,an (−109≤𝑎𝑖≤109−109≤ai≤109) — the elements of the array.

    It is guaranteed that the sum of 𝑛n across all test cases does not exceed 2⋅1052⋅105.

    Output

    For each test case, output "YES" (without quotes) if 𝑎a 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).

    Example

    input

    Copy

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

    output

    Copy

    YES
    NO
    YES
    NO
    

    Note

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

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

    In the third test case, 𝑎𝑖+𝑎𝑗+𝑎𝑘=0ai+aj+ak=0 for all distinct 𝑖i, 𝑗j, 𝑘k, and 00 is an element of the array, so the array is 3SUM-closed.

    菜得离谱,理解错题意了:

    要求是给你一个数组,求证数组中的任意三个元素之和在数组中存在这样一个数。

    首先是如果有三个及以上的正数那么肯定存在三个最大的number之和大于数组中最大的元素,反之,如果三个为负的元素,最小的三个数的和也肯定比数组中最小的数小,不符合题意。

    所以首先统计>0和<0的数量,如果有一个大于二, 就直接输出No就行

    如果存在三个及以上的0, 和两个零的效果是一样的,所以复杂度是O(n+6^4) or O(n+6^3)

    1. #include <iostream>
    2. #include <vector>
    3. #include <algorithm>
    4. #include <cstring>
    5. #include <map>
    6. using namespace std;
    7. inline int read(){
    8. int res = 0 , flag = 1 ;
    9. char c = getchar() ;
    10. while(!isdigit(c)){
    11. if(c == '-') flag = -1 ;
    12. c = getchar() ;
    13. }
    14. while(isdigit(c)){
    15. res = (res << 1) + (res << 3) + (c ^ 48) ;
    16. c = getchar() ;
    17. }
    18. return res * flag ;
    19. }
    20. void solved() {
    21. int n;
    22. int cnt1 = 0, cnt2 = 0;
    23. n = read();
    24. vector<int> arr1; // postive number
    25. vector<int> arr2; // negative number
    26. vector<int> a;
    27. for (int i = 0; i < n; i++) {
    28. int x;
    29. x = read();
    30. if (x > 0) arr1.push_back(x);
    31. if (x < 0) arr2.push_back(x);
    32. if (x == 0 && a.size() < 2) a.push_back(x);
    33. }
    34. if (arr1.size() > 2 || arr2.size() > 2) {
    35. cout << "No" << endl;
    36. return ;
    37. }
    38. for (auto i : arr1) a.push_back(i);
    39. for (auto i : arr2) a.push_back(i);
    40. for (int i = 0; i < a.size(); i++) {
    41. for (int j = i + 1; j < a.size(); j++) {
    42. for (int k = j + 1; k < a.size(); k++) {
    43. bool ok = false;
    44. for (int l = 0; l < a.size(); l++) {
    45. if (a[i] + a[j] + a[k] == a[l]) {ok = true;}
    46. }
    47. if (!ok) {cout << "NO\n"; return;}
    48. }
    49. }
    50. }
    51. cout << "YES\n";
    52. }
    53. int main() {
    54. int _;
    55. cin >> _;
    56. while (_ --) {
    57. solved();
    58. }
    59. return 0;
    60. }

  • 相关阅读:
    【BOOST C++ 19 应用库】(3)Boost.Archive
    【面试题-Vue】常见问题二、组件类
    uboot启动linux kernel的流程
    Word文档中如何添加带打勾的方框
    Navicat 连接 SQL Server 报错(IM002 未发现数据源名称并且未指定默认驱动程序)
    java-php-python-ssm校园驿站计算机毕业设计
    试试这2个流动图片制作方法让你的图片动起来吧
    数组的去重
    C++模板函数
    刷题之路:1216 - 【基础】数塔问题(递推求解)题解
  • 原文地址:https://blog.csdn.net/beloved_yu/article/details/125524048