Cod sursa(job #3183173)

Utilizator MateiAlex24Diamandi Matei MateiAlex24 Data 10 decembrie 2023 20:32:23
Problema Secventa 2 Scor 10
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.61 kb
#include <iostream>
#include <fstream>

using namespace std;

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

int main()
{
    int n, v[50002], k, suma_max=0;
    fin>>n>>k;
    for (int i=1; i<=n; i++){
        fin>>v[i];
        suma_max += v[i];
    }
    
    int poz1=1, poz2=n, poz1max=1, poz2max=n, suma;
    // 0 -6 2 1 4 -1 3 -5
    // idee: stergem ultimul/primul element depinde care e mai mic
    // while (poz2-poz1 >= k){
    //     int suma=0;
    //     for (int i=poz1; i<=poz2; i++){
    //         suma += v[i];
    //     }
    //     if (suma > suma_max){
    //         suma_max = suma;
    //         poz1max = poz1;
    //         poz2max = poz2;
    //     }
    //     if (v[poz1] > v[poz2])
    //         poz2--;
    //     else
    //         poz1++;
    // }
    
    for (int i=2; i<=n; i++){
        for (int j=i+k; j<=n; j++){
            suma = 0;
            for (int k=i; k<=j; k++){
                suma+=v[k];
            }
            if (suma > suma_max){
                suma_max = suma;
                poz1max = i;
                poz2max = j;
            } else if (suma == suma){
                if (poz1max > i){
                    suma_max = suma;
                    poz1max = i;
                    poz2max = j; 
                } else if ((poz1max == i) && (poz2max-poz1max > j-i)){
                    suma_max = suma;
                    poz1max = i;
                    poz2max = j;                     
                }
            }
        }
    }
    
    fout<<poz1max<<" "<<poz2max<<" "<<suma_max;

    return 0;
}