Pagini recente » Cod sursa (job #3289423) | Cod sursa (job #1099921) | Cod sursa (job #2291813) | Cod sursa (job #2584286) | Cod sursa (job #2419754)
#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, rightI;
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];
vector<int> st;
rightI.assign(N_MAX, N + 1);
for (int i { 1 }; i <= N; ++i) {
while (!st.empty() && a[st.back()] > a[i]) {
rightI[st.back()] = i;
st.pop_back();
}
st.push_back(i);
}
st.clear();
leftI.assign(N_MAX, 0);
for (int i { N }; i >= 1; --i) {
while (!st.empty() && a[st.back()] > a[i]) {
leftI[st.back()] = i;
st.pop_back();
}
st.push_back(i);
}
}
void solve() {
for (int i { 1 }; i <= N; ++i) {
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();
}