#2T 90pts 求卡常
查看原帖
#2T 90pts 求卡常
362022
Wildchesse楼主2023/8/12 17:10

用了两个堆,时间复杂度O(nlgn)但T在第二个点,悬关求卡常

#include<bits/stdc++.h>
#define int long long
#define endl '\n'
#define MAXN 1000005
using namespace std;
struct node{
	int pos,val;
	bool operator<(const node &x)const{
		return val>x.val;
	}
	bool operator>(const node &x)const{
		return val<x.val;
	}
}a[MAXN];
int n,k,num=n-k+1,mi[MAXN],ma[MAXN],tag=1;
priority_queue<node> q1;
priority_queue<node,vector<node>,greater<node> > q2;
signed main(){
//	ios::sync_with_stdio(false);
//	cin.tie(NULL);
//	cout.tie(NULL);
	cin>>n>>k;
	num=n-k+1;
	for(int i=1;i<=n;i++){
		scanf("%lld",&a[i].val);
		a[i].pos=i;
	}
	for(int i=1;i<=k;i++){
		q1.push(a[i]);
		q2.push(a[i]);
	}
	for(int i=1;i<=num;i++){
		while(q1.top().pos<tag){
			q1.pop();
		}
		while(q2.top().pos<tag){
			q2.pop();
		}
		mi[i]=q1.top().val;
		ma[i]=q2.top().val;
		q1.push(a[k+i]);
		q2.push(a[k+i]);
		tag++;
	}
	for(int i=1;i<=num;i++){
  		printf("%lld ",mi[i]);
	}
	cout<<endl;
	for(int i=1;i<=num;i++){
		printf("%lld ",ma[i]);
	}
	return 0;
}

2023/8/12 17:10
加载中...