Pagini recente » Cod sursa (job #2101829) | Rating Bianca Patrulescu (BiancaPatrulescu) | Cod sursa (job #300828) | Cod sursa (job #3211050) | Cod sursa (job #358619)
Cod sursa(job #358619)
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
int main() {
deque<pair <long, long> > deq;
FILE *f1=fopen("secventa.in", "r"), *f2=fopen("secventa.out", "w");
long n, k, mat, i, max, min, p, start;
fscanf(f1, "%ld%ld%ld", &n, &k, &mat);
deq.push_back(make_pair(mat, 1));
for(i=2; i<=k; i++) {
fscanf(f1, "%ld", &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++) {
fscanf(f1, "%ld", &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; }
}
fprintf(f2, "%ld %ld %ld\n", start, start+k-1, max);
fclose(f1); fclose(f2);
return 0;
}