Pagini recente » Cod sursa (job #1318193) | Cod sursa (job #1592282) | Cod sursa (job #516602) | Cod sursa (job #2825052) | Cod sursa (job #2419765)
#include <fstream>
#include <vector>
using namespace std;
const string FILE_NAME = "secventa";
const int N_MAX { 500005 };
ifstream in { FILE_NAME + ".in" };
ofstream out { FILE_NAME + ".out" };
int N, K;
vector<int> a(N_MAX), leftI(N_MAX), rightI(N_MAX);
int st[N_MAX], top;
int sol { -(1 << 30) }, solStart, solEnd;
void init() {
ios_base::sync_with_stdio(false);
in.tie(0);
in >> N >> K;
for (int i { 1 }; i <= N; ++i)
in >> a[i];
for (int i { 1 }; i <= N; ++i) {
while (top && a[st[top]] > a[i])
rightI[st[top--]] = i;
st[++top] = i;
}
top = 0;
for (int i { N }; i >= 1; --i) {
while (top && a[st[top]] > a[i])
leftI[st[top--]] = i;
st[++top] = i;
}
}
void solve() {
for (int i { 1 }; i <= N; ++i) {
if (!rightI[i])
rightI[i] = N + 1;
if (rightI[i] - 1 - leftI[i] < K || a[i] < sol)
continue;
int l { i - leftI[i] <= K ? leftI[i] + 1 : i - K + 1 }, r { l + K - 1 };
if (a[i] > sol || (a[i] == sol && l < solStart) || (a[i] == sol && l == solStart && r < solEnd)) {
sol = a[i];
solStart = l;
solEnd = r;
}
}
}
void print() {
out << solStart << ' ' << solEnd << ' ' << sol;
}
int main() {
init();
solve();
print();
}