Cod sursa(job #2838584)

Utilizator CiuiGinjoveanu Dragos Ciui Data 24 ianuarie 2022 11:01:12
Problema Secventa 2 Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
 
ifstream fin("secv2.in");
ofstream fout("secv2.out");
 
const int MAX_SIZE = 50001;
int sp[MAX_SIZE];
 
int main() {
    int n, v[MAX_SIZE], k;
    fin >> n >> k;
    for (int i = 1; i <= n; ++i) {
        fin >> v[i];
        sp[i] = sp[i - 1] + v[i];
    }
    int min = 0, maxSum = -1250000001, minIndex = 0, startIndex = 0, stopIndex = 0;
    for (int i = 1; i <= n; ++i) {
        if (sp[i] > maxSum && i >= k) {
            maxSum = sp[i];
            startIndex = 1;
            stopIndex = i;
        }
        if (sp[i] - min > maxSum && i - minIndex >= k) {
            maxSum = sp[i] - min;
            startIndex = minIndex + 1;
            stopIndex = i;
        }
        if (min > sp[i]) {
            min = sp[i];
            minIndex = i;
        }
    }
    fout << startIndex << " " << stopIndex << " " << maxSum;
}
/*
// test 12
am facut urmatoarea observatie:
{1}
1 1 -> 0
2 2 -> 2
3 3 -> 4
4 4 -> 6
...
10 10 -> 18
{1}
20000 2
-> 3
 
2 20000
-> 2
{1}
1 1000
-> 0
{1}
1000 1
-> 1
 
9 6
 -> 11
 
6 9
 -> 10
{1}
{1}
{1}
*/