• D. Corrupted Array


    D. Corrupted Array

    time limit per test

    2 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You are given a number nn and an array b1,b2,…,bn+2b1,b2,…,bn+2, obtained according to the following algorithm:

    • some array a1,a2,…,ana1,a2,…,an was guessed;
    • array aa was written to array bb, i.e. bi=aibi=ai (1≤i≤n1≤i≤n);
    • The (n+1)(n+1)-th element of the array bb is the sum of the numbers in the array aa, i.e. bn+1=a1+a2+…+anbn+1=a1+a2+…+an;
    • The (n+2)(n+2)-th element of the array bb was written some number xx (1≤x≤1091≤x≤109), i.e. bn+2=xbn+2=x; The
    • array bb was shuffled.

    For example, the array b=[2,3,7,12,2]b=[2,3,7,12,2] it could be obtained in the following ways:

    • a=[2,2,3]a=[2,2,3] and x=12x=12;
    • a=[3,2,7]a=[3,2,7] and x=2x=2.

    For the given array bb, find any array aa that could have been guessed initially.

    Input

    The first line contains a single integer tt (1≤t≤1041≤t≤104). Then tt test cases follow.

    The first line of each test case contains a single integer nn (1≤n≤2⋅1051≤n≤2⋅105).

    The second row of each test case contains n+2n+2 integers b1,b2,…,bn+2b1,b2,…,bn+2 (1≤bi≤1091≤bi≤109).

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

    Output

    For each test case, output:

    • "-1", if the array bb could not be obtained from any array aa;
    • nn integers a1,a2,…,ana1,a2,…,an, otherwise.

    If there are several arrays of aa, you can output any.

    Example

    input

    Copy

    4
    3
    2 3 7 12 2
    4
    9 1 7 1 6 5
    5
    18 2 2 3 2 9 2
    3
    2 6 9 2 1
    

    output

    Copy

    2 3 7 
    -1
    2 2 2 3 9 
    1 2 6 
    

    事先让n=n+2

    枚举x的位置,取剩下n-1个数的最值,看是否等于n-2个数的和即可,排序与前缀和处理,很水的D

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. using namespace std;
    12. typedef long long int ll;
    13. ll a[200000+10];
    14. ll sum[200000+10];
    15. int main()
    16. {
    17. int t ;
    18. cin>>t ;
    19. while(t--)
    20. {
    21. int n;
    22. cin>>n;
    23. for(int i=1;i<=n+2;i++)
    24. {
    25. cin>>a[i];
    26. }
    27. sort(a+1,a+1+n+2);
    28. for(int i=1;i<=n+2;i++)
    29. {
    30. sum[i]=sum[i-1]+a[i];
    31. }
    32. int flag=0;
    33. int pos;
    34. n+=2;
    35. for(int i=1;i<=n;i++)
    36. {
    37. if(i!=n)
    38. {
    39. ll l=sum[i-1];
    40. ll r=sum[n-1]-sum[i];
    41. if(l+r==a[n])
    42. {
    43. flag=1;
    44. pos=i;
    45. break;
    46. }
    47. }
    48. else
    49. {
    50. if(sum[n-2]==a[n-1])
    51. {
    52. flag=1;
    53. pos=n;
    54. break;
    55. }
    56. }
    57. }
    58. if(flag==0)
    59. {
    60. cout<<-1<
    61. }
    62. else
    63. {
    64. if(pos==n)
    65. {
    66. for(int i=1;i<=n-2;i++)
    67. {
    68. cout<" ";
    69. }
    70. cout<
    71. }
    72. else
    73. {
    74. for(int i=1;i
    75. {
    76. cout<" ";
    77. }
    78. for(int i=pos+1;i
    79. {
    80. cout<" ";
    81. }
    82. cout<
    83. }
    84. }
    85. }
    86. return 0;
    87. }

  • 相关阅读:
    Spring MVC 和 @ModelAttribute 注释
    keil debug查看变量提示not in scope
    Jmeter接口测试,关联接口实现步骤(token)
    TCP流量控制和拥塞控制
    并查集(数据结构)
    不同路径数(冬季每日一题 4)
    Java日志系列——日志门面,阿里日志规约,SLF4J
    湖仓一体电商项目(十六):业务实现之编写写入ODS层业务代码
    C和指针 第13章 高级指针话题 13.2 高级声明
    使用Colossal-AI进行优化的测试笔记
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126180283