Pagini recente » Cod sursa (job #320180) | Cod sursa (job #2289377) | Cod sursa (job #1288727) | Cod sursa (job #1056648) | Cod sursa (job #358635)
Cod sursa(job #358635)
#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, 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);
while(!deq.empty() && mat<=deq.back().first) {
deq.pop_back();
}
deq.push_back(make_pair(mat, i));
}
start=1; max=deq.front().first;
for(i=k+1; i<=n; i++) {
fscanf(f1, "%ld", &mat);
/**
while(!deq.empty() && deq.front().second<=i-k) {
deq.pop_front();
}
**/
if(deq.front().second<=i-k) { deq.pop_front(); }
while(!deq.empty() && mat<=deq.back().first) {
deq.pop_back();
}
deq.push_back(make_pair(mat, i));
if(deq.front().first>max) { max=deq.front().first; start=deq.front().second; }
}
fprintf(f2, "%ld %ld %ld\n", start, start+k-1, max);
fclose(f1); fclose(f2);
return 0;
}