• B. Difference Array--Codeforces Round #808 (Div. 1)


    Problem - B - Codeforces

    B. Difference Array

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    You are given an array aa consisting of nn non-negative integers. It is guaranteed that aa is sorted from small to large.

    For each operation, we generate a new array bi=ai+1−aibi=ai+1−ai for 1≤i

    After performing n−1n−1 operations, nn becomes 11. You need to output the only integer in array aa (that is to say, you need to output a1a1).

    Input

    The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1041≤t≤104) — the number of test cases. The description of the test cases follows.

    The first line of each test case contains one integer nn (2≤n≤1052≤n≤105) — the length of the array aa.

    The second line contains nn integers a1,a2,…,ana1,a2,…,an (0≤a1≤…≤an≤5⋅1050≤a1≤…≤an≤5⋅105) — the array aa.

    It is guaranteed that the sum of nn over all test cases does not exceed 2.5⋅1052.5⋅105, and the sum of anan over all test cases does not exceed 5⋅1055⋅105.

    Output

    For each test case, output the answer on a new line.

    Example

    input

    Copy

    5
    3
    1 10 100
    4
    4 8 9 13
    5
    0 0 0 8 13
    6
    2 4 8 16 32 64
    7
    0 0 0 0 0 0 0
    

    output

    Copy

    81
    3
    1
    2
    0
    

    Note

    To simplify the notes, let sort(a)sort⁡(a) denote the array you get by sorting aa from small to large.

    In the first test case, a=[1,10,100]a=[1,10,100] at first. After the first operation, a=sort([10−1,100−10])=[9,90]a=sort⁡([10−1,100−10])=[9,90]. After the second operation, a=sort([90−9])=[81]a=sort⁡([90−9])=[81].

    In the second test case, a=[4,8,9,13]a=[4,8,9,13] at first. After the first operation, a=sort([8−4,9−8,13−9])=[1,4,4]a=sort⁡([8−4,9−8,13−9])=[1,4,4]. After the second operation, a=sort([4−1,4−4])=[0,3]a=sort⁡([4−1,4−4])=[0,3]. After the last operation, a=sort([3−0])=[3]a=sort⁡([3−0])=[3].

    一道细节题,细节很多

    重点是0的讨论

    0 0 0 8 13为例

    初始

    三个零 + 8  13

    差分  两个零  8  5

    排序 两个零, 5 8

    差分 一个零 5 3

    排序 一个零 3 5

    差分 没有0 3 2

    排序  没有0 2 3

    差分 1

    我们宏观统计序列0的个数,一旦有0,就以为我们差分时第一个非零数字是要被加进去的。规律是差分一次零就会减少1,且差分过程中会出现零。

    一个致命的细节是,等差数列,在差分中全变成0,需要特判。

    1. #include
    2. using namespace std;
    3. const int N=1e5+100;
    4. int T,n;
    5. int a[N],b[N];
    6. int main()
    7. {
    8. int t;
    9. cin>>t;
    10. while(t--)
    11. {
    12. int n;
    13. cin>>n;
    14. int cnt=0,len=0;
    15. memset(a,0,sizeof(a));
    16. memset(b,0,sizeof(b));
    17. for(int i=1; i<=n; i++)
    18. {
    19. int x;
    20. cin>>x;
    21. if(!x)
    22. cnt++;
    23. else
    24. {
    25. len++;
    26. a[len]=x;
    27. }
    28. }
    29. while(len>1)
    30. {
    31. int newl=0;
    32. if(cnt)
    33. {
    34. newl++;
    35. cnt--;
    36. b[newl]=a[1];
    37. }
    38. for(int i=2; i<=len; i++)
    39. {
    40. if(a[i]-a[i-1])
    41. {
    42. newl++;
    43. b[newl]=a[i]-a[i-1];
    44. }
    45. else
    46. {
    47. cnt++;
    48. }
    49. }
    50. sort(b+1,b+1+newl);
    51. for(int i=1; i<=newl; i++)
    52. {
    53. a[i]=b[i];
    54. }
    55. len=newl;
    56. }
    57. if(len==1)
    58. cout<1]<
    59. else
    60. {
    61. cout<<0<//特判全部清空
    62. }
    63. }
    64. return 0;
    65. }

  • 相关阅读:
    【系统架构设计师考试大纲】
    PPL攻击详解
    Linux reset子系统
    大数据课程L4——网站流量项目的Hive离线批处理
    表数据结构变动、修复表数据的历史版本兼容解决方案
    解密JavaScript的异步机制:打破单线程限制,提升性能与用户体验
    Java线程池
    自动控制原理5.2---典型环节与开环系统的频率特性
    【运维心得】如何进行应用日志分析?
    csdn最新最全pytest系列——pluggy插件源码解读(一)HookspecMarker类和HookimplMarker类分析
  • 原文地址:https://blog.csdn.net/jisuanji2606414/article/details/126085718