CF1341B Nastya and Door 题解

  • 题目
    • 链接
    • 字面描述
      • 题面翻译
      • 题目描述
      • 输入格式
      • 输出格式
      • 样例 #1
        • 样例输入 #1
        • 样例输出 #1
      • 提示
  • 小建议
  • 思路
  • 代码实现

题目

链接

https://www.luogu.com.cn/problem/CF1341B

字面描述

题面翻译

给定一个长度为 nnn 的数组aiaiai。定义一个位置 iii 是“峰”,当且仅当 ai−1≤ai≥ai+1a_i−1≤a_i≥a_i+1ai​−1≤ai​≥ai​+1。

求长度为 kkk 的区间中含有“峰”个数最多的区间。

多测.

1≤t≤1041≤t≤1041≤t≤104

3≤k≤n≤2×1053≤k≤n≤2×1053≤k≤n≤2×105

0≤ai≤1090≤ai≤1090≤ai≤109

∑n≤2×105∑n≤2×105∑n≤2×105

题目描述

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 $ .

输入格式

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 $ .

输出格式

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

样例输入 #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.

小建议

看英文版题目,中文版翻译的少了很多细节甚至有错。

思路

典型的固定长度子序列的前缀和问题,主要记录峰的数量,打擂台留峰最多且角标最小的,最后直接+1(求被峰隔断的数量)。

还需要注意到的问题是峰的构成由峰本身和相邻的两个元素构成缺一不可,所以求区间和时,要2边元素下标向区间缩小1

代码实现

#include<bits/stdc++.h>
using namespace std;const int maxn=2e5+10;
int t,n,k,l,ans;
int a[maxn],x[maxn],f[maxn];
int main(){scanf("%d",&t);while(t--){scanf("%d%d",&n,&k);ans=0;for(int i=1;i<=n;i++){scanf("%d",&a[i]);x[i]=0;}//记录峰for(int i=2;i<n;i++){if(a[i]>=a[i-1]&&a[i]>=a[i+1])x[i]=1;}//求峰的前缀和for(int i=1;i<=n;i++)f[i]=f[i-1]+x[i];//打擂台for(int i=1;i<=n-k+1;i++){int p=f[i+k-2]-f[i]+1;//向区间内部缩1if(p>ans){l=i;ans=p;} } printf("%d %d\n",ans,l);} return 0;
}

CF1341B Nastya and Door 题解相关推荐

  1. Codeforces Round #637 (Div. 2) - Thanks, Ivan Belonogov! D. Nastya and Scoreboard题解(记忆化搜索)

    题目链接 题目大意 一个n个数码位的分数板,每一个数码位都是一个七段数码管,现在给出每个数码位的显示情况,问再点亮k段数码管的话能显示的最大的数是多少,如果不能构成一串数字,就输出-1.答案允许有前导 ...

  2. Codeforces Round #637 (Div. 2) C. Nastya and Strange Generator 题解(阅读理解+简单思维)

    题目链接 题目大意 真难读 问给定的序列能不能用题中所给的算法生成. 比如,题目中举的例子:原序列a: [ 2 3 * * 1 ],先得出 r 数组 [ 3, 3 ,3 ,4 , * ] .r 数组的 ...

  3. [JS][dfs]题解 | #迷宫问题#

    题解 | #迷宫问题# 题目链接 迷宫问题 题目描述 定义一个二维数组 N*M ,如 5 × 5 数组下所示: int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 1, 1 ...

  4. [JS][dp]题解 | #打家劫舍(一)#

    题解 | #打家劫舍(一)# 题目链接 打家劫舍(一) 题目描述 描述 你是一个经验丰富的小偷,准备偷沿街的一排房间,每个房间都存有一定的现金,为了防止被发现,你不能偷相邻的两家,即,如果偷了第一家, ...

  5. [JS]题解 | #魔法数字#

    题解 | #魔法数字# 题目链接 魔法数字 题目描述 牛妹给牛牛写了一个数字n,然后又给自己写了一个数字m,她希望牛牛能执行最少的操作将他的数字转化成自己的. 操作共有三种,如下: 在当前数字的基础上 ...

  6. [JS]题解 | #岛屿数量#

    题解 | #岛屿数量# 题目链接 岛屿数量 题目描述 时间限制:1秒 空间限制:256M 描述 给一个01矩阵,1代表是陆地,0代表海洋, 如果两个1相邻,那么这两个1属于同一个岛.我们只考虑上下左右 ...

  7. [JS] 题解:提取不重复的整数

    题解:提取不重复的整数 https://www.nowcoder.com/practice/253986e66d114d378ae8de2e6c4577c1 时间限制:1秒 空间限制:32M 描述 输 ...

  8. 洛谷-题解 P2672 【推销员】

    独门思路!链表加优先队列! 这题一望,贪心是跑不掉了,但是我贪心并不好,所以想到了一个复杂一些但思路更保稳的做法 思路: 1 因为是离线操作,所以我们可以倒着求,先求x=n的情况,因为那样直接就知道了 ...

  9. [洛谷1383]高级打字机 题解

    题解 这道题一看就珂以用主席树啊 这是一道神奇的题目,那么我们先敲一个主席树,然后维护一个数组len,表示下一次应该在len + 1插入, 之后对于T操作,在上一个版本的len + 1上直接执行插入 ...

最新文章

  1. qtp9.2测试java_QTP的使用举例说明
  2. AI居然能算出情侶能交往多久?使用分析语音数据進行預測
  3. 提高WPF程序性能的几条建议
  4. android xml解析demo,Android解析自定义xml文件--Sax解析xml文件,测试demo(方案二)...
  5. mysql导入表格,txt操作(以及常见问题解决方法)
  6. 共模干扰和差模干扰(图解)---摘自: 硬件十万个为什么
  7. 除了 Python ,这些语言写的机器学习项目也很牛
  8. 小型数字系统---运动码表设计
  9. python自定义切片_自定义Python切片,请指教
  10. Tensorboard--模型可视化工具
  11. vue项目token放在哪里_关于vue动态菜单的那点事
  12. leveldb使用指南
  13. 【shell系列】之awk简单介绍
  14. 基于SSM的实验室预约系统
  15. “逃离北上广”——你以为回到小城市就很幸福了么?
  16. Linux防火墙配置工具iptables中MASQUERADE的含义
  17. 华硕服务器怎么装win7系统教程,华硕win7系统重装教程
  18. win32 窗口 绘制矩形
  19. 读《高手》有感(包括问题、质疑、专业名词、哲理句)
  20. 正试图在 os 加载程序锁内执行托管代码

热门文章

  1. 《java与模式》中模式总结
  2. js垃圾回收机制,内存泄露和内存溢出,解决闭包产生的内存泄露详解
  3. 数电课设数字钟设计(基于quartus)
  4. 云主机安装redis
  5. CAXA电子图板2013机械版下载及安装教程详解
  6. Android录制声音文件(音频),并播放
  7. 免费的云服务器,大家推荐哪个呢?
  8. TestBird《2021中国证券测试白皮书》
  9. c语言编程代码大全(c语言简单代码大全)
  10. $.each与$().each