Beacon Transmission
"Title description"
Beacon, also known as Beacon, is an important defensive facility, usually built on the strategically located or traffic arteries. As soon as the enemy occurs, the daytime burns firewood, and the smoke expresses the message: The night burns dry wood, and the fire passes through the flames. There are n beacons between the two cities, and each beacon signal has a price. In order for the intelligence to be transmitted accurately, at least one of the M beacons should be signaled. Now enter the price of N, m and each beacon signal, please calculate the total minimum cost, in order to make the enemy attack, the intelligence can be in between the two cities accurate transmission!!!
"Input description"
The first line has two number n,m (1<=n,m<=1000000) respectively represents N Beacon, at least one signal in the M beacon.
The second action n number, indicating the cost of each beacon.
"Output description"
A number, which is the minimum cost.
"Sample Input"
5 3
1 2 5) 6 2
"Sample Output"
4
Analysis:
Adding two more points represents two cities as a beacon at a cost of 0, and it's easy to write DP formulas:
F[i]:=min (F[j]) +a[i] (i-m<=j<i)
Then, with the monotone queue optimization, the queue element holds the value of the F array and maintains a monotonically increasing queue, each time it takes the team head.
Code 1 (dp+ monotone queue):
ProgramFire ;varf,a,b,g:Array[0..1000001] ofLongint; N,i,m,h,t:longint;procedureWork (x:longint);beginT:=t+1; B[T]:=F[X]; g[t]:=x; while(b[t]<=b[t-1]) and(T>H) Do beginT:=t-1; b[t]:=b[t+1]; g[t]:=g[t+1]; End; ifX-g[h]=m Thenh:=h+1;End;beginassign (input,'fire.in'); reset (input); Assign (output,'Fire.out'); rewrite (output); READLN (N,M); fori:=1 toN Doread (a[i]); N:=n+1; f[0]:=0; b[1]:=0; g[1]:=0; h:=1; t:=1; fori:=1 toN Do beginF[i]:=b[h]+A[i]; Work (i); End; Writeln (F[n]); Close (input); Close (output);End.
View Code
can also be used to optimize the heap, each time it is taken to determine whether the root node out of range, out of the delete, continue to judge, until the requirements are met.
Code 2 (dp+ heap):
ProgramFire ;varf,a,b,g:Array[0..2000001] ofLongint; N,i,m,t:longint;functionGet (X:longint): Longint;varI,s,tmp:longint;begin while(x-g[1]>M) and(t>1) Do beginb[1]:=B[T]; g[1]:=g[t];t:=t-1; I:=1; while(i*2<=T)or(i*2+1<=T) Do begin if(i*2+1>T)or(b[i*2]<b[i*2+1]) Thens:=i*2 Elses:=i*2+1; ifB[i]>b[s] Then begintmp:=b[i]; B[i]:=b[s]; b[s]:=tmp; TMP:=g[i]; G[i]:=g[s]; g[s]:=tmp; I:=s; End ElseBreak ; End; End; Get:=b[1];End;procedureput (x:longint);varS,tmp:longint;beginT:=t+1; b[t]:=f[x];g[t]:=x; s:=T; while(s<>1) and(B[sDiv 2]>b[s]) Do begintmp:=b[sDiv 2]; B[sDiv 2]:=b[s]; b[s]:=tmp; TMP:=g[sDiv 2]; G[sDiv 2]:=g[s]; g[s]:=tmp; S:=sDiv 2; End;End;beginassign (input,'fire.in'); reset (input); Assign (output,'Fire.out'); rewrite (output); READLN (N,M); fori:=1 toN Doread (a[i]); N:=n+1; f[0]:=0; b[1]:=0; g[1]:=0; t:=1; fori:=1 toN Do beginF[i]:=get (i) +A[i]; Put (i); End; Writeln (F[n]); Close (input); Close (output);End.
View Code
Exercise: Beacon Transmission (dp+ monotone queue)