Cod sursa(job #2299868)

Utilizator ruxiTTutunaru Ruxandra ruxiT Data 10 decembrie 2018 12:17:27
Problema Secventa 2 Scor 90
Compilator c-64 Status done
Runda Arhiva de probleme Marime 0.82 kb
#include <stdio.h>
#include <stdlib.h>

int sum[50000];

int main() {
    int n, k, i, l, x, sc, sf, inc, lmax, stc, stmax, drmax, drc, smax;
    FILE *fin, *fout;
    fin = fopen("secv2.in", "r");
    fout = fopen("secv2.out", "w");
    fscanf(fin, "%d%d", &n, &k);
    sc = 0;
    l = 0;
    stc = stmax = drmax = 1;
    smax = -200000000;
    for (int i = 1; i <= n; i++) {
        fscanf(fin, "%d", &x);
        sum[i] = sum[i - 1] + x;
        if (i > k && sum[i] - sum[i - k] > sc + x) {
            sc = sum[i] - sum[i - k];
            stc = i - k + 1;
        }
        else {
            sc += x;
        }
        if (sc > smax && i >= k) {
            smax = sc;
            stmax = stc;
            drmax = i;
        }
    }
    fprintf(fout, "%d %d %lld", stmax, drmax, smax);
    fclose(fin);
    fclose(fout);
    return 0;
}