Pagini recente » Cod sursa (job #2630129) | Cod sursa (job #1913530) | Cod sursa (job #2694381) | Cod sursa (job #998800) | Cod sursa (job #2675708)
#include <fstream>
#include <deque>
#include <stack>
using namespace std;
ifstream cin("secventa.in");
ofstream cout("secventa.out");
deque < int > deq;
stack < int > st;
const int NMAX=5005;
int v[NMAX], st1[NMAX], dr[NMAX];
int main()
{
int n, i, j, k, maxx=0, poz;
cin >> n >> k;
for( i=1; i<=n; i++ )
{
cin >> v[i];
while( !deq.empty() and v[deq.back()] > v[i] )
deq.pop_back();
deq.push_back( i );
if( deq.front() <= i - k )
deq.pop_front();
if( i>=k )
if( v[deq.front()] > maxx )
{
maxx = v[deq.front()];
poz = deq.front();
}
}
for ( i=1; i<=n; i++)
{
int h = i;
while( !st.empty() and v[st.top()] > v[i] )
{
h = st1[st.top()];
st.pop();
}
if( st.empty() )
{
st.push( i );
st1[i] = h;
}
else
{
st1[i] = h;
st.push( i );
}
}
for ( i=n; i>=1; i--)
{
int h = i;
while( !st.empty() and v[st.top()] > v[i] )
{
h = dr[st.top()];
st.pop();
}
if( st.empty() )
{
st.push( i );
dr[i] = h;
}
else
{
dr[i] = h;
st.push( i );
}
}
cout << st1[poz] << " " << dr[poz] << " " << maxx;
return 0;
}