• 803_div2(Rising Sand, 接受军训!


    B. Rising Sand

    time limit per test

    1 second

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    There are 𝑛n piles of sand where the 𝑖i-th pile has 𝑎𝑖ai blocks of sand. The 𝑖i-th pile is called too tall if 1<𝑖<𝑛1<i<n and 𝑎𝑖>𝑎𝑖−1+𝑎𝑖+1ai>ai−1+ai+1. That is, a pile is too tall if it has more sand than its two neighbours combined. (Note that piles on the ends of the array cannot be too tall.)

    You are given an integer 𝑘k. An operation consists of picking 𝑘k consecutive piles of sand and adding one unit of sand to them all. Formally, pick 1≤𝑙,𝑟≤𝑛1≤l,r≤n such that 𝑟−𝑙+1=𝑘r−l+1=k. Then for all 𝑙≤𝑖≤𝑟l≤i≤r, update 𝑎𝑖←𝑎𝑖+1ai←ai+1.

    What is the maximum number of piles that can simultaneously be too tall after some (possibly zero) operations?

    Input

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

    The first line of each test case contains two integers 𝑛n and 𝑘k (3≤𝑛≤2⋅1053≤n≤2⋅105; 1≤𝑘≤𝑛1≤k≤n) — the number of piles of sand and the size of the operation, respectively.

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

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

    Output

    For each test case, output a single integer — the maximum number of piles that are simultaneously too tall after some (possibly zero) operations.

    Example

    input

    Copy

    3
    5 2
    2 9 2 4 1
    4 4
    1 3 2 1
    3 1
    1 3 1
    

    output

    Copy

    2
    0
    1
    

    Note

    In the first test case, we can perform the following three operations:

    • Add one unit of sand to piles 11 and 22: [3,10,2,4,1][3,10,2,4,1].
    • Add one unit of sand to piles 44 and 55: [3,10,2,5,2][3,10,2,5,2].
    • Add one unit of sand to piles 33 and 44: [3,10,3,6,2][3,10,3,6,2].

    Now piles 22 and 44 are too tall, so in this case the answer is 22. It can be shown that it is impossible to make more than 22 piles too tall.

    In the second test case, any operation will increase all piles by 11 unit, so the number of too tall piles will always be 00.

    In the third test case, we can increase any pile by 11 unit of sand. It can be shown that the maximum number of too tall piles is 11.

    这道题的意思是给你一堆沙子,然后每堆沙子有一个高度,给你一个数值k,代表的是我们接下来进行操作的范围,我们每次可以对r - l + 1 = k, 即  l<= i <= r的这一个范围里的沙子让他们的高度加一,操作可以进行无数次,然后要我们求too tall 的pile.

    too tall 的定义是,a[i] > a[i - 1] + a[i + 1] , 不能是首尾的沙子。

    代码:

    1. #include "bits/stdc++.h"
    2. using namespace std;
    3. inline int read(){
    4. int res = 0 , flag = 1 ;
    5. char c = getchar() ;
    6. while(!isdigit(c)){
    7. if(c == '-') flag = -1 ;
    8. c = getchar() ;
    9. }
    10. while(isdigit(c)){
    11. res = (res << 1) + (res << 3) + (c ^ 48) ;
    12. c = getchar() ;
    13. }
    14. return res * flag ;
    15. }
    16. void solved() {
    17. int n, k;
    18. n = read();
    19. k = read();
    20. //vector<bool> st(n + 1);
    21. vector<int> q(n + 1);
    22. for (int i = 1; i <= n; i++) {
    23. q[i] = read();
    24. }
    25. if (k == 1) {
    26. cout << (n - 1) / 2 << "\n";
    27. return ;
    28. } else {
    29. int res = 0;
    30. for (int i = 2; i < n; i++) {
    31. res += (q[i] > q[i - 1] + q[i + 1]);
    32. }
    33. cout << res << "\n";
    34. return ;
    35. }
    36. }
    37. int main() {
    38. int _;
    39. cin >> _;
    40. while (_ --) {
    41. solved();
    42. }
    43. return 0;
    44. }

    如果k等于1, 也就是说,我们可以让任意三个相邻的中间那个数变成无穷大,使得它变成too tall,也就是说我们最大可以得到(n - 1) / 2向下取整。比如4个数,只能有一个too tall, 首尾不算。

    如果k不等于一的话,可以画图看一下,不管你怎么变化,too tall的数量永远都是只能比一开始的too tall的数量少,因为我们是同时对一串进行变化,其中中间那一部分靠着的肯定是一块变化的,只能减少, 比如k取三的时候1 5 3 4变成 2644或者1 5 4 5, 第二个就不是too tall了, 数量就变成0了,而后三个不管怎么变化a[2]和a[3]永远都是同时加一,也就是说a[2] + 1 + a[4] < a[3] + 1 不会影响结果,因此k >= 2的时候最初的状态有多少too tall piles就是结果。

  • 相关阅读:
    国密GmSSL v2版本命令行方式生成国密sm2私钥、公钥、签名和验证签名
    Java面试题基础第六天
    Windows 下 Qt 可执行程序添加默认管理员权限启动(QMAKE、MinGW & MSVC)
    unity urp 衣服渲染
    探索arkui(2)--- 布局(列表)--- 1(列表数据的展示)
    业务需要咨询?开发遇到 bug 想反馈?开发者在线提单功能上线!
    备忘录模式(Memento Pattern)
    k8s集群CD工具-ArgoCD
    JavaScript-DOM编程
    vulnhub靶场之THALES: 1
  • 原文地址:https://blog.csdn.net/beloved_yu/article/details/125519301