Cod sursa(job #1486215)

Utilizator alexandru.ghergutAlexandru-Gabriel Ghergut alexandru.ghergut Data 14 septembrie 2015 11:58:57
Problema Secventa Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.71 kb
#include <fstream>
#include <iostream>
#include <set>

using namespace std;

int main()
{
    int N, K, i, maximum;
    multiset<int> tree;
    ifstream f("secventa.in");
    f >> N >> K;
    int a[N], lastIndex = 0, maxIndex = -1;
    for (i = 0; i < N; i++)
        f >> a[i];
    f.close();
    for (i = 0; i < N; i++)
    {
        tree.insert(a[i]);
        if (tree.size() > K)
            tree.erase(a[lastIndex++]);
        if (tree.size() == K && ( maxIndex == -1 || *tree.begin() > maximum))
            maximum = *tree.begin(), maxIndex = lastIndex;
    }

    ofstream g("secventa.out");
    g << maxIndex + 1 << " " << maxIndex + K << " " << maximum;
    g.close();
    return 0;
}