Cod sursa(job #825098)

Utilizator cristalCiurdarean Andrei cristal Data 27 noiembrie 2012 14:48:04
Problema Secventa 2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.73 kb
#include <fstream>
using namespace std;
#define INF 0x3f3f3f3f

ifstream is("secv2.in");
ofstream os("secv2.out");

int a[1002];
int n, k;

int main()
{
    is >> n >> k;
    for ( int i = 0; i < n; i++)
        is >> a[i];

    int s =  0;       // suma secventei curente
    int Smax = -INF;
    int St, Dr;
    int p1 = 0, p2 = 0;  // pozitiile stanga si dreapta a capetelor secventei curente
    for ( int i = 0; i < n; i++)
    {
		s += a[i];
		if ( s >= 0 )
        {
			p2 = i;
            if ( s > Smax )
            {
				Smax = s;
				St = p1, Dr = p2;
			}
		}
		else
		{
			s = 0;
			p1 = i + 1;
        }
    }
    if ( p2 - p1 >= k )
        os << Dr << " " << St << " " << Smax << '\n';

    is.close();
    os.close();
    return 0;
}