637(Div.2)B. Nastya and Door(前缀和)

题目描述

On February 14, Denis decided to give a 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≥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 a1,a2,…,an in order from left to right (k≤n). It is guaranteed that neighboring heights are not equal to each other (that is, ai≠ai+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, ai−1ai+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≤l≤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.

Input

The first line contains an integer t (1≤t≤104) — 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≤k≤n≤2⋅105) — the number of mountains and the length of the door.
The second line of the input data set contains n integers a1,a2,…,an (0≤ai≤109, ai≠ai+1) — the heights of mountains.
It is guaranteed that the sum of n over all the test cases will not exceed 2⋅105.

Output

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.

Example

input
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
output
3 2
2 2
2 1
3 1
2 3

Note

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.

题目大意

把一个贼长的门(这个门长到可以覆盖k座山)扔到了群山(有连着的n座山)上,门如果掉到山峰上就会断掉成两半。某座山的高度比左右的两座山都要高就叫山峰。现在给出n座山的高度a[i]和k,求门最多可以断成几节和此时的门的最左边在第几座山上。

题目分析

这道题难度不大,就是先遍历一边求出并用数组记录所有山峰的位置。
然后遍历每一个(i,i+k-1)区间中山峰的个数(不算端点!!),从而算出最大能断成几节。
每一个区间都循环一次的话时间复杂度是O(nk),会超时。因此我们需要用一个前缀和优化,这样时间复杂度就优化为了O(n)。

代码如下
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
#include <iomanip>
#define LL long long
using namespace std;
const int N=2e5+5;
int a[N];
int st[N];    //记录山峰
int main()
{
    int t;
	cin>>t;
	while(t--)
	{
		int n,k;
		cin>>n>>k;
		for(int i=1;i<=n;i++)
		cin>>a[i];
		memset(st,0,sizeof st);  //清空st数组
		for(int i=2;i<n;i++)     //遍历求出山峰,并进行前缀和优化
		{
			if(a[i]>a[i-1]&&a[i]>a[i+1]) st[i]=1;
			st[i]+=st[i-1];
		}
		int ans=0,l;     //用ans记录最大能断成几节,l记录此时的左端点
		for(int i=1;i+k-1<=n;i++)
		{  //cnt记录门在(i,i+k-1)([i+1,i+k-2])区间上时能断成几节
			int cnt=st[i+k-2]-st[i]+1; 
		    if(cnt>ans) ans=cnt,l=i;
		}
		cout<<ans<<' '<<l<<endl;
	}
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值