Home > Back-end >  , the problem of circular array maximum continuous period and why it is wrong to write like this
, the problem of circular array maximum continuous period and why it is wrong to write like this

Time:09-23

Question:
Cyclic sequence of N integers a [1], a [2], [3], a... , the sequence as a [n], a [I] + [I + 1) + a... + a [j] continuous period and the maximum value (the number refers to the n cycle sequence into a circle, so you need to consider a [n - 1], a [n], a [1], [2] such a sequence), when the integer are given negative and 0,
For example: - 2, 4, 13, 5, 2, and the largest child period is: 11, 4, 13, and is 20,
Input
Line 1: the integer sequences of length N (2 & lt;=N
=50000)Line 2 - N + 1: N integers (9-10 ^ & lt; [I]=S & lt; 10 ^=9)
The Output
Circular array output of the architectural segment and



#include
#include
using namespace std;
The vector & lt; int> a;
The vector & lt; int> b;
Int main ()
{
int n;
cin> n;
A.r esize (n);
int i,j;
for(i=0; icin> A, [I].
B.r esize (n);
B=a;
Anderson, nsert (a.e nd (), b.b do v.begin (), v.end ());//connect the original array fore and aft, constitute a double array
Int t=0;
Int t1=0, t2=0;
Long long int sum=0;
Long long int Max=0;
for(i=0; i<2 * n. I++)
{
The sum +=a [I];
T++;
If (t> N)//t says there have been several Numbers together, when t> N is should do processing
{
//cout<& lt;" Tiaole "& lt; & lt;" I "& lt; //cout<& lt;" Xunhuan "& lt; The sum -=a (t1);
T -;
T1 + +;
While (a (t1) & lt;=0)
{
The sum -=a (t1);
T -;
T1 + +;
}
}
If (max<=sum)
{

Max=sum;
T2=I;
}
//cout<& lt;" Sum="& lt; If (sum<=0)
{
sum=0;
T=0;
T1=I + 1;
}

//cout<& lt;" Sum="& lt;