好神奇
查看原帖
好神奇
781159
Lovely_Elaina楼主2024/8/15 20:16

AC 代码,由暴力和正解赞助得到。

#include <bits/stdc++.h>
#define int long long
//#define double long double
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define endl '\n' 
using namespace std;
typedef long long ll;
const int N = 1e6+5;
const int P = 998244353;

int n,m,a[N];
int ans = 0x7f7f7f7f;
int q[N],h,t;

signed main(){
	ios::sync_with_stdio(0);
	cin.tie(NULL);
	
// 	freopen("app.in","r",stdin);
	
	cin >> n >> m;
	for(int i = 1; i <= n; i++)
		cin >> a[i];
	
	if(n <= 5000){
		while(m--){
			for(int i = 1; i < n; i++)
				a[i] = max(a[i],a[i+1]);
			n--;
		}
		
		while(n > 1){
			for(int i = 1; i < n; i++)
				a[i] = min(a[i],a[i+1]);
			n--;
		}
		
		cout << a[1] << endl;
		return 0;
	}
	
	for(int i = 1; i <= n; i++){
		while(h <= t && q[h] < i-m+1) h++;
		while(h <= t && a[i] > a[q[t]]) t--;
		q[++t] = i;
		if(i > m) ans = min(a[q[h]],ans);
	}
	
	cout << ans << endl;
	return 0; 
} 

但是去掉暴力会 WA

https://www.luogu.com.cn/record/172895298

2024/8/15 20:16
加载中...