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:
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] , 不能是首尾的沙子。
代码:
-
- #include "bits/stdc++.h"
-
- using namespace std;
-
- inline int read(){
- int res = 0 , flag = 1 ;
- char c = getchar() ;
- while(!isdigit(c)){
- if(c == '-') flag = -1 ;
- c = getchar() ;
- }
- while(isdigit(c)){
- res = (res << 1) + (res << 3) + (c ^ 48) ;
- c = getchar() ;
- }
- return res * flag ;
- }
-
- void solved() {
- int n, k;
- n = read();
- k = read();
- //vector<bool> st(n + 1);
- vector<int> q(n + 1);
- for (int i = 1; i <= n; i++) {
- q[i] = read();
- }
-
- if (k == 1) {
- cout << (n - 1) / 2 << "\n";
- return ;
- } else {
- int res = 0;
- for (int i = 2; i < n; i++) {
- res += (q[i] > q[i - 1] + q[i + 1]);
- }
- cout << res << "\n";
- return ;
- }
- }
- int main() {
- int _;
- cin >> _;
- while (_ --) {
- solved();
- }
- return 0;
- }
如果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就是结果。