Cod sursa(job #1769832)

Utilizator tiberiu.bucur17Tiberiu Constantin Emanoil Bucur tiberiu.bucur17 Data 3 octombrie 2016 11:33:12
Problema Subsecventa de suma maxima Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 1 kb
#include <stdio.h>
#include <ctype.h>
#define lim 131072
FILE *fin,*fout;
char BUF[lim];
int poz=lim;
inline char getch()
{
    if(poz==lim)
    {
        fread(BUF,lim,1,fin);
        poz=0;
    }
    return BUF[poz++];
}
inline int getnr()
{
    char ch;
    ch=getch();
    while(isdigit(ch)==0 && ch!='-')
        ch=getch();
    int semn=1,rez=0;
    if(ch=='-')
    {
        semn=-1;
        ch=getch();
    }
    do
    {
        rez=rez*10+ch-'0';
        ch=getch();
    } while(isdigit(ch));
    return rez*semn;
}
int main()
{
    fin=fopen("ssm.in","r");
    fout=fopen("ssm.out","w");
    int n,i,sc,x,max,start,stop,st;
    sc=0;max=-2000000000;
    st=1;
    n=getnr();
    for(i=0;i<n;i++)
    {
        x=getnr();
        if(sc<0)
        {
            sc=0;
            st=i+1;
        }
        sc+=x;
        if(sc>max)
        {
            max=sc;
            stop=i+1;
            start=st;
        }
    }
    fprintf(fout,"%d %d %d",max,start,stop);
    fclose(fin);
    fclose(fout);
    return 0;
}