Cod sursa(job #2687817)

Utilizator YusyBossFares Yusuf YusyBoss Data 20 decembrie 2020 10:53:54
Problema Subsecventa de suma maxima Scor 95
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.74 kb
#include <iostream>
#include <stdio.h>

using namespace std;

int main() {
  FILE *fin, *fout;
  int n, i, last, left, right, x, smax, leftmax, rightmax;

  fin = fopen("ssm.in", "r");
  fscanf(fin, "%d", &n);

  fscanf(fin, "%d", &x);
  last = x;
  left = right = leftmax = rightmax = 0;
  smax = x;

  for (i = 1; i < n; i++) {
    fscanf(fin, "%d", &x);
    if (last > 0) {
      last += x;
      right++;
    }
    else {
      last = x;
      left = right = i;
    }

    if (last > smax) {
      smax = last;
      leftmax = left;
      rightmax = right;
    }
  }
  fclose( fin );

  fout = fopen("ssm.out", "w");
  fprintf(fout, "%d %d %d", smax, leftmax + 1, rightmax + 1);
  fclose( fout );
  return 0;
}