Codeforces Round # 585 (Div. 2) E (state compression DP, thinking)

HAVE_STRUCT_TIMESPEC #define
#include <bits / STDC ++ H.>
the using namespace STD;
int A [400007];
int C [400007];
Long Long F [(20 is <<. 1) +7];
Long Long DP [(<<. 1 20 is) +7] [27];
int CNT [27];
int main () {
int n-;
CIN n->>;
for (int I =. 1; I <= n-; I ++) {
CIN >> A [ I];
c [I] = a [I];
}
Sort (. 1 + c, c + n-+. 1); // c copied to the array and a sort
int m = unique (c + 1 , c + 1 + n) -c-1; // find the c number of different elements of the array
for (int I =. 1; I <= n-; ++ I)
a [I] = lower_bound (. 1 + c, c + +. 1 m, a [i]) - c; // change the original elements of a first array to appear
for (int I =. 1; I <= n-; I ++) {
++ CNT [a [I]]; first appeared // count the number of elements in the position
for (int. 1 = J; J <= m; J ++) {
IF (CNT [J] == a || J [I]!)
Continue;
dp [1 << (j-1 )] [a [i]] + = cnt [j]; // number appears in a [i] before a [j] is (the a [i] storing the original contribution to move all colors before a [j] stored in the original color made)
}
}
for (int I = 0; I <<<. 1 m; I ++)
for (int. 1 = J; J <= m ; ++ J)
DP [i] [J] = DP [i & (- i)] [J] + DP [i ^ (i & (- i))] [J]; // i is the bit that is 1 to move all colors to the color contribution of j is equal to i before removing the contribution of a last sequence i 1 plus a contribution last 1
memset (f, 127/3, sizeof (f)), f [0 ] = 0;
for (int I = 0; I <<<. 1 m; I ++) {
IF (F [I] == F [m <<. 1])
Continue;
for (int J = 0; J < m; j ++)
(! (i & j >>. 1) IF) // If i is moved to the right position j is an even number (the last bit is 0), there is described an intermediate color should not be moved to its upper position
f [i | (1 << j)] = min (f [i] + dp [i] [j + 1], f [i | (1 << j)]); // j from right to left on the bit 0 up to 1, and its own contribution to f [i] + dp [i ] [j + 1] is the minimum value (i sequence plus the contribution of the i sequences are all moved to the previous color contribution J + 1)
}
cout << f [(1 << m ) -1] << endl; // all permutations contribution arrangement is intended to answer the question of a
return 0;
}

Guess you like

Origin www.cnblogs.com/ldudxy/p/11528693.html