Title Description:
N the number of rows in a row, you can arbitrarily select a number of consecutive numbers, calculate their and. Ask how to choose the ability
Makes the sum of the absolute minimum.
For example: n=8, 8 numbers are as follows:
1 2 3 4 5 6 7 8
-20 90-30-20 80-70-60 125
If we choose 1 to 4 of these 4 numbers, and for 20, you can also choose 6 to 8 of these 3 numbers, and for -5,|-5|=5,
The scheme obtains the lowest absolute value.
Input format:
The first line enters N, which indicates the number of digits. The next n lines describe the n numbers.
Output format:
The first line outputs an integer that represents the sum of the minimum absolute value, and the second row contains an integer representing the absolute value
And the length of the longest sequence.
Data Description:
40% of Data n<=4000
For many data, the length of the longest sequence is unique.
100% data n<=100000,| The value of each number |<=10^10
Input:
8
-20
90
-30
-20
80
-70
-60
125
Output:
5
3
Ideas: 1: Brute force enumeration, enumeration of the first endpoint, tail end, and then with O (n) time to calculate and, Time complexity O (n^3); explode the sky.
2: Optimization method One, the use of prefixes and ideas, with O (1) to calculate the sum of L to R, the total time O (n^2); It seems to be still not working.
3: Positive Solution: Think scenario 2, for each R, we want to find a l to make L prefix and-r prefix and minimum, and then we want to quickly find,
Think about how the absolute value of the two-digit subtraction is the smallest for a sequence? Obviously, the size of the adjacent two number, because if the number of intervals, must not be inferior to the size of the number of excellent
With this idea, we can sort the obtained prefixes and arrays, then subtract the two adjacent numbers by 22, so that the time complexity of O (1) can be used to calculate the results.
1 Programex01;2 varA,F,PL:Array[0..100100] ofInt64;3 N,ans,ll:int64;4 functionMax (a,b:longint): Longint;5 begin6 ifA>b Thenexit (a);7 exit (b);8 End;9 functionmin (a,b:longint): Longint;Ten begin One ifA<b Thenexit (a); A exit (b); - End; - procedureInit; the varI:longint; - begin - READLN (n); - fori:=1 toN Do + begin - readln (A[i]); +f[i]:=f[i-1]+A[i]; Apl[i]:=i; at End; - End; - procedureqsort (l,r:longint); - varI,j,p,mid:int64; - begin -I:=l; J:=r; mid:=f[(I+J)Div 2]; in Repeat - whileF[i]<mid DoInc (i); to whileF[j]>mid DoDec (j); + ifI<=j Then - begin theP:=f[i]; F[I]:=F[J]; f[j]:=p; *P:=pl[i]; PL[I]:=PL[J]; pl[j]:=p; $ Inc (I); Dec (j);Panax Notoginseng End; - untilI>J; the ifI<r Thenqsort (i,r); + ifL<j Thenqsort (l,j); A End; the proceduredoit; + varI:longint; - begin $ans:=Maxlongint; $ fori:=2 ton+1 Do - begin - ifABS (f[i]-f[i-1]) =abs (ANS) Then the ifMax (pl[i],pl[i-1])-min (pl[i],pl[i-1]) >ll Then -Ll:=max (pl[i],pl[i-1])-min (pl[i],pl[i-1]);Wuyi ifABS (f[i]-f[i-1]) <abs (ANS) Then the begin -Ans:=abs (f[i]-f[i-1]); WuLl:=max (pl[i],pl[i-1])-min (pl[i],pl[i-1]); - End; About End; $ End; - procedurePrint; - begin - writeln (ans); A Writeln (ll); + End; the begin - Init; $Qsort1, n+1); the doit; the Print; the the End.
Min and (min)