• C. 3SUM Closure(规律搜寻)


    原题链接:

    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+akai+aj+ak is an element of the array. More formally, aa is 3SUM-closed if for all integers 1≤i<j<k≤n1≤i<j<k≤n, there exists some integer 1≤l≤n1≤l≤n such that ai+aj+ak=alai+aj+ak=al.

    Determine if aa is 3SUM-closed.

    Input

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

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

    The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (−109≤ai≤109−109≤ai≤109) — the elements of the array.

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

    Output

    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).

    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 i=1i=1, j=2j=2, k=3k=3. In this case, a1+a2+a3=0a1+a2+a3=0, which is an element of the array (a2=0a2=0), so the array is 3SUM-closed.

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

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

    思路:

    1,看数据范围,暴力过不去

    2,如果正的多于2个,和一定大于任何2个,如果负的多于2个,和一定小于任何两个,0最多有2个,多了没有用。分类讨论。

    3,暴力剩下的数,复杂度O(n+6^4);

    代码:

    1. #include<bits/stdc++.h>
    2. using namespace std;
    3. #define int long long
    4. const int maxj=2e5+100;
    5. int a[maxj];
    6. void solve(){
    7. int n;
    8. cin>>n;
    9. vector<int>x,y,z;
    10. map<int,int>mp;
    11. for(int i=1;i<=n;++i){
    12. cin>>a[i];
    13. if(a[i]>0)x.emplace_back(a[i]);
    14. else if(a[i]<0)y.emplace_back(a[i]);
    15. else if(z.size()<2)z.emplace_back(a[i]);
    16. mp[a[i]]=1;
    17. }if(x.size()>2||y.size()>2){
    18. cout<<"NO"<<'\n';
    19. return ;
    20. }
    21. for(auto i:x)z.emplace_back(i);
    22. for(auto i:y)z.emplace_back(i);
    23. for(int i=0;i<z.size();++i){
    24. for(int j=i+1;j<z.size();++j){
    25. for(int k=j+1;k<z.size();++k){
    26. int x=z[i]+z[j]+z[k];
    27. if(!mp[x]){
    28. cout<<"NO"<<'\n';
    29. return ;
    30. }
    31. }
    32. }
    33. }cout<<"YES"<<'\n';
    34. }
    35. int32_t main(){
    36. ios::sync_with_stdio(0);
    37. cin.tie(0);
    38. cout.tie(0);//ios与其他加速方式不可混用
    39. int t;
    40. cin>>t;
    41. while(t--)solve();
    42. return 0;
    43. }

  • 相关阅读:
    【攻破css系列——第九天】常规流
    Web前端:2022年每个开发人员都必须遵循的React最佳实践
    【算法】【递归与动态规划模块】矩阵的最小路径和
    ECharts实现数据可视化 “ 10分钟入门 “ 教程(超详细)
    Wise Care 365 Pro v6.3.9.617 系统优化软件
    光线投射之伪3d
    【input系统】MotionEvent的分解
    Windows Server 2012服务器无法识别ADB Interface的解决办法
    MySQL最基本的常识
    小白系统初始化配置资源失败怎么办
  • 原文地址:https://blog.csdn.net/m0_63054077/article/details/125554386