Hdu_3506
Because it is a ring, you can copy the sequence first, then write the state transition equation f [I] [J] = min {f [I] [k-1] + F [k] [J]} + W [I] [J], where W is the time sum of I to J, which is the same as the state transition equation in the case of quadrilateral inequality in the black book. Therefore, we only need to prove that W is convex. Because W [I] [J] + W [I + 1] [J + 1] = W [I] [J + 1] + W [I + 1] [j], therefore, if W is convex, you can use the Quadrilateral inequality to optimize DP with confidence.
# Include <stdio. h>
# Include < String . H>
# Define Maxd 2010
# Define INF 0x3f3f3f
Int N, F [maxd] [maxd], K [maxd] [maxd], a [maxd], a [maxd];
Void Init ()
{
Int I, J, K;
A [ 0 ] = 0 ;
For (I = 0 ; I <n; I ++)
{
Scanf ( " % D " , & A [I]);
A [I] = A [I- 1 ] + A [I];
}
For (I = N; I < 2 * N; I ++)
A [I] = A [I- 1 ] + A [I-n];
}
Int Getw ( Int I, Int J)
{
Return A [J]-A [I- 1 ];
}
Int Getmin ( Int X, Int Y)
{
Return X <Y? X: Y;
}
Void Solve ()
{
Int I, J, K, P, T, ans;
For (I = 0 ; I <= 2 * N; I ++)
{
F [I] [I] = 0 ;
K [I] [I] = I;
}
For (P = 1 ; P <n; P ++)
For (I = 1 ; (J = I + p) <= 2 * N; I ++)
{
F [I] [J] = inf;
For (K = K [I] [J- 1 ]; K <= K [I + 1 ] [J]; k ++)
{
T = f [I] [k- 1 ] + F [k] [J] + getw (I, j );
If (T <F [I] [J])
{
F [I] [J] = T;
K [I] [J] = K;
}
}
}
Ans = inf;
For (I = 1 ; I <= N; I ++)
Ans = getmin (F [I] [I + N- 1 ], ANS );
Printf ( " % D \ n " , ANS );
}
Int Main ()
{
While (Scanf ( " % D " , & N) = 1 )
{
Init ();
Solve ();
}
Return 0 ;
}