HDU 5009 paint pearls (Dynamic Planning)

Source: Internet
Author: User

Paint pearls

Problem descriptionlee has a string of N pearls. in the beginning, all the pearls have no color. he plans to color the pearls to make it more fascinating. he drew his ideal pattern of the string on a paper and asks for your help.

In each operation, he selects some continuous pearls and all these pearls will be painted Their target colors.When he paints a string which has k different target colors, Lee will cost K2 points.

Now, Lee wants to cost as few as possible to get his ideal string. You shoshould tell him the minimal cost.
Inputthere are multiple test cases. Please process till EOF.

For each test case, the first line contains an integer N (1 ≤ n ≤ 5 × 104), indicating the number of pearls. the second line contains A1, A2 ,..., an (1 ≤ AI ≤ 109) indicating the target color of each Pearl.
Outputfor each test case, output the minimal cost in a line.
Sample Input
31 3 3103 4 2 4 4 2 4 3 2 2

Sample output
27

Source2014 ACM/ICPC Asia Regional Xi 'an online
Recommendhujie

Question:

Given a series of colors, you can divide them into any number of intervals of any size. The cost of each interval is the square of the number of colors. What is the minimum total cost?


Solution:

With dynamic planning, the two-way linked list maintains different elements and deletes the same elements.

My reference is: http://blog.csdn.net/u011345136/article/details/39759935

Solution code:

# Include <iostream> # include <map> # include <cstdio> using namespace STD; const int maxn = 51000; int N, d [maxn], DP [maxn], pre [maxn], next [maxn]; Map <int, int> MP; // subscript corresponding to the MP Record Number // pre record precursor // next record subsequent void solve () {MP. clear (); For (INT I = 1; I <= N; I ++) {pre [I] = I-1; next [I] = I + 1; DP [I] = (1 <30);} DP [0] = 0; Pre [0] =-1; for (INT I = 1; I <= N; I ++) {If (MP. find (d [I]) = MP. end () MP [d [I] = I; else {int id = MP [d [I]; next [pre [ID] = next [ID]; Pre [next [ID] = pre [ID]; MP [d [I] = I;} int C = 0; for (Int J = pre [I]; J! =-1; j = pre [J]) {C ++; DP [I] = min (DP [I], DP [J] + C * C ); if (C * C> = I) break;} printf ("% d \ n", DP [N]);} int main () {While (scanf ("% d", & N )! = EOF) {for (INT I = 1; I <= N; I ++) scanf ("% d", & D [I]); solve ();} return 0 ;}



HDU 5009 paint pearls (Dynamic Planning)

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.