HDU 3506 monkey party

Source: Internet
Author: User

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 ;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.