Từ Trường THPT chuyên Hoàng Lê Kha, Tây Ninh
Thông tin
Main Languages
Tools & Infrastructure
https://lichess.org/JMfAN6BZ
#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
freopen("chess.inp","r",stdin);
freopen("chess.out","w",stdout);
stack<ll> st;
ll n;
cin>>n;
vector<ll> a(n+100),pr(n+100);
for(ll i=1;i<=n;i++)
cin>>a[i];
a[0]=LLONG_MIN;
st.push(0);
for(ll i=1;i<=n;i++)
{
while(a[i]<=a[st.top()])
st.pop();
pr[i]=st.top()+1;
st.push(i);
}
for(ll i=1;i<=n;i++)
cout<<pr[i]<<" ";
cout<<'\n';
while(!st.empty())
st.pop();
a[n+1]=LLONG_MIN;
st.push(n+1);
for(ll i=n;i>=1;i--)
{
while(a[i]<=a[st.top()])
st.pop();
pr[i]=st.top()-1;
st.push(i);
}
for(ll i=1;i<=n;i++)
cout<<pr[i]<<" ";
return 0;
}
https://github.com/1234coco/ZaloCustomTheme
Insomniacs After School