Cod sursa(job #843714)

Utilizator ericptsStavarache Petru Eric ericpts Data 28 decembrie 2012 11:24:35
Problema Buline Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <cstdio>
using namespace std;
const int maxn = 200002;

char *p;

void read(int &a)
{
    a&=0;
    while(*p && (*p > '9' || *p < '0'))
        ++p;
    while(*p >= '0' && *p <= '9')
        a = a * 10 + *(p++)-48;
}
int v[maxn];
int best[maxn << 1];
int main()
{
    freopen("buline.in","r",stdin);
    freopen("buline.out","w",stdout);
    int len;
    fseek(stdin,0,SEEK_END);
    len = ftell(stdin);
    p = new char[len];
    rewind(stdin);
    fread(p,1,len,stdin);
    int N,i;
    int semn;
    read(N);
    for(i=1;i<=N;++i)
    {
        read(v[i]);
        read(semn);
        if(!semn)
            v[i]= -v[i];
        best[i] = best[i-1]+v[i];
    }
    for(;i<=(N<<1);++i)
    {
        best[i] = best[i-1]+v[i-N];
    }
    int minsum = 0,show_sum = -(1<<30),idx=0,show_start,show_end;
    for(i=1;i<=(N<<1);++i)
    {
        if(show_sum < best[i] - minsum)
        {
            if((i > N && i-N < idx) || i <= N)
            {
                show_sum = best[i] - minsum;
                show_start = idx+1;
                show_end = i;
            }
        }
        if(minsum > best[i])
        {
            minsum = best[i];
            idx = i;
        }
    }
    printf("%d %d %d\n",show_sum,show_start,show_end-show_start+1);
    return 0;
}