Pagini recente » Cod sursa (job #2086269) | Cod sursa (job #1619165) | Cod sursa (job #1475730) | Cod sursa (job #1938676) | Cod sursa (job #358615)
Cod sursa(job #358615)
#include <iostream>
#include <fstream>
#include <deque>
#include <algorithm>
using namespace std;
int main() {
deque<pair <long, long> > deq;
fstream f1, f2;
long n, k, mat, i, max, min, p, start;
f1.open("secventa.in", ios::in);
f1>>n>>k>>mat;
deq.push_back(make_pair(mat, 1));
for(i=2; i<=k; i++) {
f1>>mat;
p=deq.back().first;
while(!deq.empty() && mat<=p) {
deq.pop_back();
p=deq.back().first;
}
deq.push_back(make_pair(mat, i));
}
min=deq.front().first; start=1;
max=min;
for(i=k+1; i<=n; i++) {
f1>>mat;
p=deq.front().second;
while(!deq.empty() && p<=i-k) {
deq.pop_front();
p=deq.front().second;
}
p=deq.back().first;
while(!deq.empty() && mat<=p) {
deq.pop_back();
p=deq.back().first;
}
deq.push_back(make_pair(mat, i));
min=deq.front().first;
if(min>max) { max=min; start=deq.front().second; }
}
f1.close();
f2.open("secventa.out", ios::out);
f2<<start<<" "<<start+k-1<<" "<<max;
f2.close();
return 0;
}