• [思维]The Enchanted Forest Codeforces1688D


    Marisa comes to pick mushrooms in the Enchanted Forest.

    The Enchanted forest can be represented by nn points on the XX-axis numbered 11 through nn. Before Marisa started, her friend, Patchouli, used magic to detect the initial number of mushroom on each point, represented by a1,a2,…,ana1,a2,…,an.

    Marisa can start out at any point in the forest on minute 00. Each minute, the followings happen in order:

    • She moves from point xx to yy (|x−y|≤1|x−y|≤1, possibly y=xy=x).
    • She collects all mushrooms on point yy.
    • A new mushroom appears on each point in the forest.

    Note that she cannot collect mushrooms on minute 00.

    Now, Marisa wants to know the maximum number of mushrooms she can pick after kk minutes.

    Input

    Each test contains 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 two integers nn, kk (1≤n≤2⋅1051≤n≤2⋅105, 1≤k≤1091≤k≤109) — the number of positions with mushrooms and the time Marisa has, respectively.

    The second line of each test case contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) — the initial number of mushrooms on point 1,2,…,n1,2,…,n.

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

    Output

    For each test case, print the maximum number of mushrooms Marisa can pick after kk minutes.

    Example

    input

    4

    5 2

    5 6 1 2 3

    5 7

    5 6 1 2 3

    1 2

    999999

    5 70000

    1000000000 1000000000 1000000000 1000000000 1000000000

    output

    12

    37

    1000000

    5000349985

    题意: 在一个n格的地图上,有若干蘑菇,每格初始蘑菇数为ai,现在可以任选一个起点,同时可以在k秒内移动,每秒移动一格,移动到某格后可以获得格子内的蘑菇数,同时所有格子蘑菇数增加1,初始时间为0秒,问到k秒时能获得的最大蘑菇数。

    分析: 当k <= n时最终答案就是长度为k的最大区间和加上k*(k-1)/2,当k > n时考虑最终状态各个格子上的蘑菇数,因为每秒全图上蘑菇数会增加n,而最终一定可以把全图的蘑菇都走遍,所以只需要计算出浪费的蘑菇数,然后用总蘑菇数一减就是能获得的蘑菇数,而要想浪费的蘑菇数最少就需要最后走到端点,此时每个格子上的蘑菇数为n,n-1,......,3,2,1可以发现其他任何方式浪费的蘑菇数都比这样多,所以可以选择在第一个格子等待,当剩余时间为n-1时一直向右走遍所有格子,这样答案就是sum+n*k-n*(n+1)/2。

    这道题如果想到了站在原地不动就会比较简单,但是当时想到的是不断来回移动,虽然答案是一样的,但是实现起来就麻烦一点了。

    具体代码如下:

    简单版

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #define int long long
    7. using namespace std;
    8. int a[200005];
    9. int c[200005];
    10. const int inf = 0x3f3f3f3f3f3f3f3f;
    11. signed main()
    12. {
    13. int T;
    14. cin >> T;
    15. while(T--){
    16. int n, k;
    17. scanf("%lld%lld", &n, &k);
    18. int sum = 0, mx = -inf;
    19. for(int i = 1; i <= n; i++){
    20. scanf("%lld", &a[i]);
    21. if(i <= k) sum += a[i];
    22. else sum = sum+a[i]-a[i-k];
    23. mx = max(mx, sum);
    24. }
    25. if(k <= n)
    26. printf("%lld\n", mx+k*(k-1)/2);
    27. else
    28. printf("%lld\n", sum+n*k-n*(n+1)/2);
    29. }
    30. return 0;
    31. }

    复杂版

    1. #include
    2. #include
    3. #include
    4. #include
    5. #include
    6. #define int long long
    7. using namespace std;
    8. int a[200005];
    9. int c[200005];
    10. const int inf = 0x3f3f3f3f3f3f3f3f;
    11. signed main()
    12. {
    13. int T;
    14. cin >> T;
    15. while(T--){
    16. int n, k;
    17. scanf("%lld%lld", &n, &k);
    18. int sum = 0, mx = -inf;
    19. for(int i = 1; i <= n; i++){
    20. scanf("%lld", &a[i]);
    21. if(i <= k) sum += a[i];
    22. else sum = sum+a[i]-a[i-k];
    23. mx = max(mx, sum);
    24. c[i] = c[i-1]+2*i;
    25. }
    26. if(n == 1){
    27. printf("%d\n", a[1]+k-n);
    28. continue;
    29. }
    30. if(k <= n)
    31. printf("%lld\n", mx+k*(k-1)/2);
    32. else{
    33. int s = k%(n-1);
    34. if(s == 0) s = n-1;
    35. int t = s*(s-1)/2;
    36. int path = k-s;
    37. printf("%lld\n", sum+t+(path/(n-1)-1)*c[n-1]+c[s-1]+(2*s-1+n+s-2)*(n-s)/2);
    38. }
    39. }
    40. return 0;
    41. }

  • 相关阅读:
    【限时免费】20天拿下华为OD笔试之【双指针】2023Q1A-最长的元音字符串【欧弟算法】全网注释最详细分类最全的华为OD真题题解
    clickhouse集群安装部署
    LeetCode50天刷题计划(Day 13—— 四树之和(8.50-10.00)
    深入C++ Vector:解密vector的奥秘与底层模拟实现揭秘
    基于 ARM + FPGA 的 EtherCAT 主站设计及实现
    [KALI] 开启ssh远程连接
    elementUI 图片全屏预览
    Hadoop的读写流程
    Matlab:使用分类数组的好处
    【零基础学QT】第九章 窗口美化QSS的使用
  • 原文地址:https://blog.csdn.net/m0_55982600/article/details/126902031