HDU 5009 paint pearls

Source: Internet
Author: User

First, point with the same color is reduced to a point, that is, data discretization.

Then, DP [I] is used to indicate the minimum cost of the first I point. For vertex I + 1, there are two cases:

1) apply it separately, that is, DP [I + 1] = DP [I] + 1

2) nodes that have been coated at a time from the K node (excluding K) to the I + 1 node, and the nodes that have been painted together have a total of num colors, that is, DP [I + 1] = DP [k] + num * num

The state transition equation DP [I + 1] = min (DP [I], DP [k] + num * num) can be obtained)

However, if you traverse each K from the back, the time-out will occur.

Therefore, we can use a two-way linked list to wear the last position of each color. If the color of a newly added vertex does not appear, add it to the end of the two-way linked list. If this point exists, delete the last position from the two-way linked list and add the latest position to the end of the two-way linked list.

It should be noted that a header node should be created so that the first node will not be deleted because of the same color, so that it cannot be calculated from start to end.

The second thing to note is that if num * num is greater than the cost I of each node, there is no need to look forward.

The Code is as follows:

1 # define maxn 50005 2 # include <stdlib. h> 3 # include <iostream> 4 # include <stdio. h> 5 # include <map> 6 # include <limits. h> 7 8 using namespace STD; 9 int arr [maxn]; // enter 10 int L [maxn]; // record the last occurrence of the color position 11 int R [maxn]; // record the last occurrence of the color position 12 int dp [maxn]; // DP [I] indicates the minimum price of 13 int m for the I node; // The length of the array after discretization is 14 15 void solve () 16 {17 Map <int, int> exist; // map stores the currently used color and the last position 18 int last = 1; // the end of the two-way linked list 19 L [0] =-1; // header Node 20 R [0] = 1; // header node 21 L [1] = 0; 22 exist [arr [1] = 1; 23 DP [0] = 0; 24 DP [1] = 1; 25 26 for (INT I = 2; I <m; I ++) 27 {28 If (exist. count (ARR [I]) = 0) 29 {30 R [last] = I; // Add it to the end of 31 L [I] = last; 32 last = I; 33 exist [arr [I] = I; 34} 35 else36 {37 int TMP = exist [arr [I]; 38 R [L [TMP] = R [TMP]; // Delete tmp39 L [R [TMP] = L [TMP]; // Delete tmp40 R [last] = I; 41 L [I] = last; 42 last = I; 43 exist [arr [I] = I; 44} 45 46 int K = last; 47 DP [I] = DP [I-1] + 1; 48 int num = 1; 49 while (L [k]> = 0) 50 {51 DP [I] = min (DP [I], DP [L [k] + num * num); 52 num ++; 53 K = L [k]; 54 if (Num * num> I) // pruning 55 {56 break; 57} 58} 59} 60 printf ("% d \ n", DP [M-1]); 61} 62 63 int main (INT argc, char * argv []) 64 {65 int N; 66 while (scanf ("% d", & N )! = EOF) 67 {68 int A; 69 m = 1; 70 for (INT I = 1; I <= N; I ++) // starts from 1, position 0 is the first node 71 {72 scanf ("% d", & A); 73 if (I = 1) 74 {75 arr [M ++] =; 76} 77 else if (ARR [M-1]! = A) // merge nodes of the same color, discretization 78 {79 arr [M ++] = A; 80} 81} 82 solve (); 83} 84 return 0; 85}

 

HDU 5009 paint pearls

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.