Cod sursa(job #1005859)

Utilizator Anonim123Andrei Marin Anonim123 Data 5 octombrie 2013 22:41:31
Problema Subsecventa de suma maxima Scor 80
Compilator cpp Status done
Runda Arhiva educationala Marime 0.95 kb
/* O(n^2)
#include<fstream>
using namespace std;
ifstream f("ssm.in");
ofstream g("ssm.out");
int n, x[100000], st, dr, poz, pozf;
int main(){
    f>>n;
    for(int i=0; i<n; ++i) f>>x[i];
    int Smax=x[0];
    for(st=0; st<n; st++)
        for(int sum=0, dr=st; dr<n; dr++){
                sum+=x[dr];
                if(sum>Smax) Smax=sum, poz=st, pozf=dr;
            }
        g<<Smax<<" "<<poz+1<<" "<<pozf+1<<'\n';

return 0;
}
*/
#include<fstream>
using namespace std;
ifstream f("ssm.in");
ofstream g("ssm.out");
int n, x[6000000], Smax, Sum, st, poz, pozf, i;
int main(){
    f>>n;
    for(int j=0; j<n; ++j)
        f>>x[j];
    for(Smax=Sum=x[0], st=poz=0, pozf=i=1; i<=n; ++i)
        if(Sum<0)
            Sum=x[i], st=i;
        else{
         Sum+=x[i];
         if(Smax<Sum)
            Smax=Sum, poz=st, pozf=i;
        }
        if(pozf==n) pozf--;
       g<<Smax<<" "<<poz+1<<" "<<pozf+1<<'\n';

return 0;
}