Cod sursa(job #2342556)

Utilizator Asgari_ArminArmin Asgari Asgari_Armin Data 12 februarie 2019 21:47:49
Problema Secventa 2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.67 kb

#include <fstream>

std::ifstream fin("secv2.in") ;
std::ofstream fout("secv2.out") ;

int n,i,maxim(1 << 31),numar,s(-1),p2,p1,p,k,l,x[100005];

int main() {
    fin>>n>>k ;
    for (i = 1 ; i <= n ; ++ i) {
        fin>>numar ;
        x[i]=x[i - 1]+numar ;
    }
    for (i = 1 ; i <= n; i++) {
        if (i>=k && s+(x[i] - x[i - 1]) < x[i] - x[i - k]) {
            s = x[i]-x[i - k];
            l=i-k+1;
        } else {
            s += (x[i] - x[i - 1]) ;
        }
        if (s > maxim && i >= k) {
            p2 = i ;
            p1 = l ;
            maxim = s ;
        }
    }
    fout << p1 << " " << p2 << " " << maxim;
    return 0;
}