Hdu 1421 move to dormitory (DP)

Source: Internet
Author: User

Question meaning:

Give you n and n numbers, and select k from them to minimize fatigue ..

Fatigue calculation method: (two-handed item quality difference square ).

Analysis: Sort items first. The minimum value is possible only when two adjacent items are taken ..

Typical DP: dp [I] [j] = min (dp [I-1] [j], dp [I-2] [J-1] + a [I-1]);

Dp [I] [j] indicates that the minimum fatigue value of j items is obtained from I items ..

Www.2cto.com
[Csharp]
# Include "stdio. h"
# Include "string. h"
# Include "algorithm"
# Define MIN (a, B) a> B? B:;
Int dp [2001] [1001];
Int main ()
{
Int n, k, I, j, a [1, 2001];
While (scanf ("% d", & n, & k )! =-1)
{
For (I = 1; I <= n; I ++)
Scanf ("% d", & a [I]);
// Use a sorting method of C ++ .. Www.2cto.com
Std: sort (a + 1, a + 1 + n );
For (I = 1; I <n; I ++)
A [I] = (a [I]-a [I + 1]) * (a [I]-a [I + 1]);
Memset (dp, 127, sizeof (dp ));
For (I = 0; I <= n; I ++) dp [I] [0] = 0;
For (I = 2; I <= n; I ++)
{
For (j = 1; 2 * j <= I; j ++)
Dp [I] [j] = MIN (dp [I-1] [j], dp [I-2] [J-1] + a [I-1]);
}
Printf ("% d \ n", dp [n] [k]);
}
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.