A13420 | Nastya and Door
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
On February 14 Denis decided to give Valentine to Nastya and did not come up with anything better than to draw a huge red heart on the door of the length $k$ ( $k \ge 3$ ). Nastya was very confused by this present, so she decided to break the door, throwing it on the mountains.
Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ( $k \le n$ ). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$ ).
Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$ ) are called indexes $i$ such that $l < i < r$ , $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$ . It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$ , then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$ ), and there are no peaks on the segment $[3, 6]$ .
To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ( $1 \le l \le n-k+1$ ). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$ , where $p$ is the number of peaks on the segment $[l,l+k-1]$ .
Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal.
Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$ .
Mountains are described by a sequence of heights $a_1, a_2, \dots, a_n$ in order from left to right ( $k \le n$ ). It is guaranteed that neighboring heights are not equal to each other (that is, $a_i \ne a_{i+1}$ for all $i$ from $1$ to $n-1$ ).
Peaks of mountains on the segment $[l,r]$ (from $l$ to $r$ ) are called indexes $i$ such that $l < i < r$ , $a_{i - 1} < a_i$ and $a_i > a_{i + 1}$ . It is worth noting that the boundary indexes $l$ and $r$ for the segment are not peaks. For example, if $n=8$ and $a=[3,1,4,1,5,9,2,6]$ , then the segment $[1,8]$ has only two peaks (with indexes $3$ and $6$ ), and there are no peaks on the segment $[3, 6]$ .
To break the door, Nastya throws it to a segment $[l,l+k-1]$ of consecutive mountains of length $k$ ( $1 \le l \le n-k+1$ ). When the door touches the peaks of the mountains, it breaks into two parts, after that these parts will continue to fall in different halves and also break into pieces when touching the peaks of the mountains, and so on. Formally, the number of parts that the door will break into will be equal to $p+1$ , where $p$ is the number of peaks on the segment $[l,l+k-1]$ .
Nastya wants to break it into as many pieces as possible. Help her choose such a segment of mountains $[l, l+k-1]$ that the number of peaks on it is maximum. If there are several optimal segments, Nastya wants to find one for which the value $l$ is minimal.
Formally, you need to choose a segment of mountains $[l, l+k-1]$ that has the maximum number of peaks. Among all such segments, you need to find the segment that has the minimum possible value $l$ .
输入格式
The first line contains an integer $t$ ( $1 \leq t \leq 10^4$ ) — the number of test cases. Then the descriptions of the test cases follow.
The first line of each test case contains two integers $n$ and $k$ ( $3 \leq k \leq n \leq 2 \cdot 10^5$ ) — the number of mountains and the length of the door.
The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ( $0 \leq a_i \leq 10 ^ 9$ , $a_i \neq a_{i + 1}$ ) — the heights of mountains.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$ .
The first line of each test case contains two integers $n$ and $k$ ( $3 \leq k \leq n \leq 2 \cdot 10^5$ ) — the number of mountains and the length of the door.
The second line of the input data set contains $n$ integers $a_1, a_2, \dots, a_n$ ( $0 \leq a_i \leq 10 ^ 9$ , $a_i \neq a_{i + 1}$ ) — the heights of mountains.
It is guaranteed that the sum of $n$ over all the test cases will not exceed $2 \cdot 10^5$ .
输出格式
For each test case, output two integers $t$ and $l$ — the maximum number of parts that the door can split into, and the left border of the segment of length $k$ that the door should be reset to.
输入输出样例
输入 #1
5 8 6 1 2 4 1 2 4 1 2 5 3 3 2 3 2 1 10 4 4 3 4 3 2 3 2 1 0 1 15 7 3 7 4 8 2 3 4 5 21 2 3 4 2 1 3 7 5 1 2 3 4 5 6 1
输出 #1
3 2 2 2 2 1 3 1 2 3
In the first example, you need to select a segment of mountains from $2$ to $7$ . In this segment, the indexes $3$ and $6$ are peaks, so the answer is $3$ (only $2$ peaks, so the door will break into $3$ parts). It is not difficult to notice that the mountain segments $[1, 6]$ and $[3, 8]$ are not suitable since they only have a $1$ peak (for the first segment, the $6$ index is not a peak, and for the second segment, the $3$ index is not a peak).
In the second example, you need to select a segment of mountains from $2$ to $4$ . In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts).
In the third example, you need to select a segment of mountains from $1$ to $4$ . In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$ , $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$ , but these segments have a left border greater than the segment $[1, 4]$ , so they are not the correct answer.
In the second example, you need to select a segment of mountains from $2$ to $4$ . In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts).
In the third example, you need to select a segment of mountains from $1$ to $4$ . In this segment, the index $3$ is a peak, so the answer is $2$ (only $1$ peak, so the door will break into $2$ parts). You can see that on the segments $[2, 5]$ , $[4, 7]$ and $[5, 8]$ the number of peaks is also $1$ , but these segments have a left border greater than the segment $[1, 4]$ , so they are not the correct answer.
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted