跳到正文

题解

题解:P10878 [JRKSJ R9] 在相思树下 III

_cyclohexane约 1 分钟
原文
文章信息与版本

文章信息

发布
2024年8月16日
收录
2025年12月12日
更新
2026年11月21日
篇幅
约 1 分钟
题目
P10878

版本

  1. v1当前

思路分析

显然,首先应该全部执行操作一,why?

Q:如果执行操作二,会发生什么?

A:可能会导致更大的大数流失,更大的数可能在开始就被淘汰掉!

所以,单调队列执行操作一,找出更大的 i−mi-m 个数,然后再在这个求出的序列中找最小值(很明显,不是吗?)

代码

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e6+5;
int n,m,s[N],rlt=0x3f3f3f3f;
struct node{
	int dat,pos;
};
deque<node>q;
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;i++)cin>>s[i];
	for(int i=1;i<=n;i++){
		while(!q.empty()&&q.front().pos<i-m)q.pop_front();
		while(!q.empty()&&q.back().dat<=s[i])q.pop_back();
		q.push_back({s[i],i});
		if(i>m)rlt=min(rlt,q.front().dat);
	}
	cout<<rlt;
	return 0;
}
原文