1660: [Usaco2006 nov]bad Hair Day Hair Festival
Time Limit:2 Sec Memory limit:64 MB
submit:665 solved:318
[Submit] [Status]
Description
Input
* Line 1: The number of cattle N.
* Lines 2..n+1: The i+1 is an integer that represents the height of the first cow.
Output
* Line 1: An integer representing c[1] to c[n] and.
Sample Input
6
10
3
7
4
12
2
Input explanation:
Six of the cows were lined up in rows of 10, 3, 7, 4, 12, 2, respectively.
Sample Output
5
3+0+1+0+1=5
HINT
Source
Silver
In fact, you can use a monotonous queue to change a bit of monotonous heap do. But I'm not proud of, hum--Build a tree line segment, after initialization as long as support a kind of moe function is enough: in the interval to find the position of the most left greater than or equal to the point of a value, such as the sample, then Cash (1,n,1,2,n,10) get is 5, And then the line of the tree is so cute (I thought it would be a small tle, can be down incredibly WA, and then a look incredibly is the array opened the TT, the final surprise of the--604ms do not explain)
1 var2 I,k,m,n:longint;3 L,j:int64;4A,b,c:Array[0..500000] ofLongint;5 functionmin (x,y:longint): Longint;6 begin7 ifX<y ThenMin:=xElsemin:=y;8 End;9 functionMax (x,y:longint): Longint;Ten begin One ifX>y ThenMax:=xElsemax:=y; A End; - procedurebuilt (x,y,z:longint); - begin the ifX=y Then - begin -a[z]:=C[x]; -b[z]:=x; + exit; - End; +Built (x, (X+y)Div 2, z*2); ABuilt ((x+y)Div 2+1, y,z*2+1); atA[z]:=max (a[z*2],a[z*2+1]); - ifa[z]=a[z*2] Thenb[z]:=b[z*2]Elseb[z]:=b[z*2+1]; - End; - functionCash (X,y,z,l,r,t:longint): Int64; - var - I,j,k:longint; in begin - ifL>r ThenExit (-1); to ifA[z]<t ThenExit (-1); + if(c[l]>=t) Thenexit (l); - if(L=R)or(x=y) ThenExit (-1); theI:=cash (x, (X+y)Div 2, z*2, L,min ((x+y)Div 2, R), T); * ifi=-1 Then $Cash:=cash ((x+y)Div 2+1, y,z*2+1, Max ((x+y)Div 2+1, L), r,t)Panax Notoginseng Else -cash:=i; the End; + begin A READLN (n); the fori:=1 toN Do + readln (C[i]); -Built1N1); $l:=0; $ fori:=1 toN-1 Do - begin -J:= (Cash (1N1, i+1, N,c[i])); the ifj=-1 ThenL:=l+int64 (Int64 (n)-int64 (i))ElseL:=l+int64 ((Int64 (j)-int64 (i))-1); - End;Wuyi Writeln (l); the End.
1660: [Usaco2006 nov]bad Hair Day Hair Festival