Pagini recente » Cod sursa (job #2715350) | Cod sursa (job #2522326)
#include <deque>
#include <fstream>
#define NMAX 500005
using namespace std;
int n, k, v[NMAX], maxBaza, resi;
class InParser
{
private:
FILE* fin;
char* buff;
int sp;
char read_ch()
{
++sp;
if (sp == 4096)
{
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume)
{
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int& n)
{
char c;
int sp = 1;
while (!isdigit(c = read_ch()) && c != '-');
if (c == '-')
{
n = 0;
sp = -1;
}
else
n = c - '0';
while (isdigit(c = read_ch()))
n = n * 10 + c - '0';
n *= sp;
return *this;
}
};
InParser fin("secventa.in");
ofstream fout("secventa.out");
int main() {
fin >> n >> k;
for(int i = 1; i <= n; i++)
fin >> v[i];
deque <int> q;
for(int i = 1; i <= k; i++) { ///adaugam primele k elemente in deque
while(!q.empty() && v[q.front()] > v[i]) ///eliminam prin fata toate elementele mai mari decat elementul curent, care devin irelevante
q.pop_front();
q.push_front(i);
}
maxBaza = v[q.back()];
resi = 1;
for(int i = k+1; i <= n; i++){
while(!q.empty() && v[q.front()] > v[i])
q.pop_front();
q.push_front(i);
while(!q.empty() && q.back() < i-k+1) ///eliminam prin spate elementele care nu mai fac parte din secventa curenta
q.pop_back();
if(v[q.back()] > maxBaza) { ///actualizam rezultatul
maxBaza = v[q.back()];
resi = i - k + 1;
}
}
fout << resi <<' '<< resi + k - 1 << ' ' << maxBaza;
}