Cod sursa(job #825122)

Utilizator bYca3Byca Blar bYca3 Data 27 noiembrie 2012 15:22:20
Problema Secventa 2 Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.79 kb
#include <fstream>
using namespace std;
#define INF 0x3f3f3f3f

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


int main()
{
    int a[1002];
    int n, k;

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

    int s =  0;       // suma secventei curente
    int Smax = -INF;
    int S, D;
    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;
				S = p1, D = p2;
			}
		}
		else
		{
			s = 0;
			p1 = i + 1;
        }
    }
    if ( p2 - p1 >= k )
    os << D << " " << S << " " << Smax << '\n';

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