Cod sursa(job #1963171)

Utilizator shantih1Alex S Hill shantih1 Data 12 aprilie 2017 12:44:07
Problema Secventa Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.33 kb
#include <iostream>
#include <fstream>
#include <deque>

using namespace std;

int n, i, j, mx=-30005, a, b, x, k, nr;
string s;
deque <int> dq, pq;

int main()
{
    ifstream fin("secventa.in");
    ofstream fout("secventa.out");

    fin >> n >> k;
    getline(fin, s);
    getline(fin, s);
    for (i = 0; i < s.size(); i++)
    {

        if (s[i] == ' ')    continue;
        if (s[i] == '-')
        {
            x = 0;
            i++;
            while (s[i] != ' ' && i < s.size())
            {
                x = x * 10 - (s[i]-'0');
                i++;
            }
        }
        else if (s[i] != ' ')
        {
            x = 0;
            while (s[i] != ' ' && i < s.size())
            {
                x = x * 10 + (s[i]-'0');
                i++;
            }
        }
        nr++;
        if (!dq.empty() && pq.front() <= nr-k)
        {
            pq.pop_front();
            dq.pop_front();
        }
        while (!dq.empty() && dq.back() >= x)
        {
            pq.pop_back();
            dq.pop_back();
        }

        pq.push_back(nr);
        dq.push_back(x);
        if (nr >= k && dq.front() > mx)
        {
            b = pq.back();  a = b-k+1;
            mx = dq.front();
        }
    }

    fout << a << " " << b << " " << mx << "\n";
}