蒟蒻自己写的垃圾板子,希望大牛指出板子的不足之处
#include <iostream>
#include <utility>
#define maxn 1000005
typedef long long ll;
using namespace std;
ll a[maxn];
pair<ll,ll> q[maxn];
ll n,m,head,tail;
void pushmax(pair<ll,ll> x)
{
if(x.second >= q[head].second)
{
head = x.first;
tail = x.first;
q[head] = x;
return ;
}
else
{
while(x.second >= q[tail].second)
{
tail--;
}
tail++;
q[tail] = x;
}
}
void pushmin(pair<ll,ll> x)
{
if(x.second <= q[head].second)
{
head = x.first;
tail = x.first;
q[head] = x;
return ;
}
else
{
while(x.second <= q[tail].second)
{
tail--;
}
tail++;
q[tail] = x;
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
pair<ll,ll> x;
ll n,m;
cin >> n >> m;
head = 1,tail = 1;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
}
q[head].first = 1;
q[head].second = a[1];
for(int i = 1; i <= n; i ++)
{
x.first = i;
x.second = a[i];
pushmin(x);
if(i > q[head].first + m - 1)head++;
if(i >= m)cout << q[head].second << " ";
}
cout << endl;
head = 1,tail = 1;
q[head].first = 1;
q[head].second = a[1];
for(int i = 1; i <= n; i ++)
{
x.first = i;
x.second = a[i];
pushmax(x);
if(i > q[head].first + m - 1)head++;
if(i >= m)cout << q[head].second << " ";
}
cout << endl;
return 0;
}
、
#include <iostream>
#include <cstdio>
#include <queue>
#include <utility>
#define maxn 1000005
typedef long long ll;
using namespace std;
ll a[maxn],n,m;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
//cin >> n >> m;
scanf("%lld%lld",&n,&m);
for(int i = 1; i <= n; i ++)
{
scanf("%lld",&a[i]);
}
deque<pair<ll,ll> > q1,q2;
q1.push_front(make_pair(1,a[1]));
for(int i = 1; i <= n; i ++)
{
if(a[i] <= q1.front().second)
{
while(!q1.empty())q1.pop_front();
q1.push_front(make_pair(i,a[i]));
}
else
{
while(a[i] <= q1.back().second)
{
q1.pop_back();
}
q1.push_back(make_pair(i,a[i]));
}
if(i > q1.front().first + m - 1)q1.pop_front();
if(i >= m)printf("%lld ", q1.front().second);
}
printf("\n");
q2.push_front(make_pair(1,a[1]));
for(int i = 1; i <= n; i ++)
{
if(a[i] >= q2.front().second)
{
while(!q2.empty())q2.pop_front();
q2.push_front(make_pair(i,a[i]));
}
else
{
while(a[i] >= q2.back().second)
{
q2.pop_back();
}
q2.push_back(make_pair(i,a[i]));
}
if(i > q2.front().first + m - 1)q2.pop_front();
if(i >= m)printf("%lld ", q2.front().second);
}
printf("\n");
return 0;
}
#include <iostream>
#include <utility>
#include <queue>
#include <deque>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll maxn = 1e6 + 5;
const ll mod = 1e9 + 7;
deque<pair<ll,ll> > q1,q2;
ll a[maxn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
ll n,m;
cin >> n >> m;
for(int i = 1; i <= n; i ++)
{
cin >> a[i];
}
for(int i = 1; i <= n; i ++)
{
while(!q1.empty() && q1.back().second >= a[i])
{
q1.pop_back();
}
q1.push_back(make_pair(i,a[i]));
if(i > q1.front().first + m - 1)q1.pop_front();
if(i >= m)cout << q1.front().second << ' ';
}
cout << '\n';
for(int i = 1; i <= n; i ++)
{
while(!q2.empty() && q2.back().second <= a[i])
{
q2.pop_back();
}
q2.push_back(make_pair(i,a[i]));
if(i > q2.front().first + m - 1)q2.pop_front();
if(i >= m)cout << q2.front().second << ' ';
}
cout << '\n';
return 0;
}