Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5009
2014 questions about Xi'an in Network Competition
Paint pearlsTime Limit: 4000/2000 MS (Java/others) memory limit: 65536/65536 K (Java/Others) Total submission (s): 1951 accepted submission (s): 631
Problem description Lee 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 paintedTheir 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. Input there 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. Output for each test case, output the minimal cost in a line. Sample input31 3 3103 4 2 4 2 4 3 2 2 Sample output27 Source 2014 ACM/ICPC Asia Regional Xi 'an online Recommendhujie | we have carefully selected several similar problems for you: 5041 5040 5039 5038 |
Question:
N numbers are given, which indicates the color to be painted in a row of pearls. At the beginning, there was no color at all. You can attach several consecutive pearls at the cost of the square of the different colors of these pearls. After this string of pearls is coated, the minimum price and.
Question:
Nlog (n) DP also requires some supercommit methods.
First we can perform some small processing, with a maximum of 5 W pearls and a maximum of 5 W colors. It gives 10 ^ 9 colors. I will use map to convert them to 5 W.
Then, several of the same colors in a row can be merged into one. After the above two processes, we get a new pearl array. Then there is the difficulty.
First look, a maximum of 233 pearls, 50000 ^ 2> 233, that is, the price of even coating has exceeded separately.
Dynamic Planning, DP [I] indicates the price required to finish the I Pearl.
DP [I] = max (DP [I], DP [k] + J * j)
J is the number of different colors until I. The maximum number of different colors J is root N, so DP is O (n * root N.
I from 1 to n, J from 1 to J * j> = I (because 1 ~ I ).
The key is how to calculate the K corresponding to each J.
If we know the K corresponding to each J of I, we can find the K corresponding to each J of I + 1. It is easy to think. The details are as follows:
Set K corresponding to J of I to K (I, j ).
B [] is a pearl array,
If B [I + 1] is a new color
K (I + 1, J + 1) = K (I, j)
K (I + 1, 1) = I + 1
If B [I + 1] is a previous color, we use a last [] array to store the last position of each color, then
For (K (I, j)> last [B [I + 1]) j, k (I + 1, J + 1) = K (I, j)
For J (K (I, j) <last [B [I + 1]), no change is made.
K (I + 1, 1) = I + 1
In this way, we can push from I to I + 1.
We can find that all the operations are to move a subscript backward by a heap of numbers. If the array directly produces the TLE. I used a simulated linked list to create a new color. When the new color appears, it is directly added to the back. When the old color appears, the demarcation point is deleted and then added to the back.
Code:
1 // # pragma comment (linker, "/Stack: 102400000,102400000 ") 2 # include <cstdio> 3 # include <cmath> 4 # include <iostream> 5 # include <cstring> 6 # include <algorithm> 7 # include <cmath> 8 # include <map> 9 # include <set> 10 # include <stack> 11 # include <queue> 12 using namespace STD; 13 # define ll long 14 # define usll unsigned ll 15 # define MZ (array) memset (array, 0, sizeof (array) 16 # define minf (array) MEm Set (array, 0x3f, sizeof (array) 17 # define rep (I, n) for (I = 0; I <(n); I ++) 18 # define for (I, X, n) for (I = (x); I <= (n); I ++) 19 # define RD (X) scanf ("% d", & X) 20 # define RD2 (x, y) scanf ("% d", & X, & Y) 21 # define rd3 (x, y, z) scanf ("% d", & X, & Y, & Z) 22 # define Wn (X) printf ("% d \ n", x); 23 # define re freopen ("xorin.txt", "r", stdin) 24 # define we freopen ("xorin.txt ", "W", stdout) 25 # define MP make_pair 26 # define Pb Pu Sh_back 27 const int maxn = 55555; 28 const int maxm = 240; 29 int B [maxn], Bn; 30 int CC [maxm + 1]; 31 int DP [maxn]; 32 int d [maxn], dn, DQ; // d [J] stores a location where the number of DN files is 1 ~ DN, the last one is in DQ; 33 int L [maxn], R [maxn]; // L [d [DN] With the subscript of the second different d, next, it is the third 34 int last [maxn]; // last [x] indicates the subscript 35 int farm () of d [] at the position where x last appeared () {36 int I, j, k; 37 // For (J, 1, bn) printf ("% 3d", B [J]); 38 // puts (""); 39 MZ (last); 40 L [0] =-1; 41 d [0] = 0; 42 B [0] = 0; 43 DN = 1; 44 DQ = 1; 45 d [1] = 1; 46 l [1] = 0; 47 R [1] = 0; 48 last [B [1] = 1; 49 for (I, 1, bn) {50 DP [I] = I; 51 j = 1; 52 for (k = L [DQ]; k! =-1; k = L [k]) {53 If (CC [J]> = DP [I]) break; 54 // printf ("DP [% d] = min (% d, % d + % d) \ n", I, DP [I], DP [d [K], CC [J]); 55 DP [I] = min (DP [I], DP [d [k] + CC [J]); 56 J ++; 57} 58 If (last [B [I + 1] = 0) {59 DN ++; 60 R [DQ] = DN; 61 L [DN] = DQ; 62 R [DN] = 0; 63 DQ = DN; 64 d [DN] = I + 1; 65 66} else {67 J = last [B [I + 1]; 68 L [R [J] = L [J]; 69 R [L [J] = R [J]; 70 L [J] = DQ; 71 R [J] = 0; 72 R [DQ] = J; 73 DQ = J; 74 d [DQ] = I + 1; 75} 76 last [B [I + 1] = DQ; 77} 78 return DP [bn]; 79} 80 81 int A [maxn]; 82 Map <int, int> S; 83 int main () {84 int N, I, T; 85 int X; 86 for (I, 0, maxm) {87 CC [I] = I * I; 88} 89 90 while (RD (n )! = EOF) {91 s. clear (); 92 int CNT = 0; 93 bn = 0; 94 for (I, 1, n) {95 RD (x); 96 int T = s [x]; 97 If (t = 0) s [x] = T = ++ CNT; 98 If (B [bn]! = T) B [++ bn] = T; 99} 100 printf ("% d \ n", farm (); 101} 102 return 0; 103}
View code
It's a little difficult. I don't understand the game for a long time, and I don't know much about it after I finish it ...... After coming up, I found that the idea should be quite simple ......
I have found many different methods on the Internet. kuangbin chicory says this is a lot of water. I don't know much about his methods of searching datasets ......
Hdu5009 paint pearls (DP + simulated linked list)