[Weekend training] marble

topic

[Test Limitations

Memory Limit: $ 256 MiB $
Time limit: $ 4000 ms $
Standard input and output
Question Types: Traditional
Evaluation methods: text comparison

Description [title]

Lin was a marble collector, his collection of $ N $ blocks of various colors of marble in the home, the $ i $ blocks of marble colors for $ a_i $.

But Lin think these stones placed at random at home too messy, he wants to put all the same color stones together.

In other words, the need for conventional Lin rearrange marble, after rearranging, for each color $ J $, if the leftmost color marble $ J $ $ L $ is the first block of marble, the rightmost color is J $ $ $ R & lt marble are $ marbles, then from L $ $ $ R & lt marbles to the first $ marbles, stones colors are $ j $.

Since these marble heavier than Lin can not bear the weight of the marble too long, so he can only swap adjacent each carry two marble.

I ask, how many times handling Lin least need to be?

[Input Format]

A first line of input digital $ N (2≤N≤4 * 10 ^ 5) $, represents the total number of marble.

The second input line numbers $ N $ $ a_1, a_2 ..., an (1≤a_i≤20) $ $ I $ denotes the color marbles $ a_i $.

[Output format]

The number of handling output requires a minimum of Lin.

[Sample input]

Sample input . 1 
. 7 
. 3  . 4  2  . 3  . 4  2  2 

Sample Input 2 
. 5 
20 is  . 1  14  10  2 

Sample input . 3 
13 is 
. 5  . 5  . 4  . 4  . 3  . 5  . 7  . 6  . 5  . 4  . 4  . 6

[Sample output]

Sample output . 1 
. 3 

sample output 2 
0 

sample output . 3 
21 is

[Problem-solving experience]

Just got this question, I was deeply attracted by its time limit: $ 4000ms $?

So long a time limit must be violent search exhaustion search algorithm ah

So after the introduction of the sample code to start the search ...

Finding that he hit the back of the search have not come out to play, so the algorithm is such a great halfway died ...

[Code] positive solutions &

One can almost be said to be like a template pressure DP title .

Look at all the data range, found $ 1≤a_i≤20 $, almost extreme opposites, $ 2≤N≤4 * 10 ^ 5 $, can be said for the $ a_i $ for $ N $ it is very big.

The time limit has enormous: $ 4000ms $.

So what does this illustrate yet?

In data entry, if the range in which one or two very small, while other data and abnormally large

And the time limit and exceptionally loose

For the time, the constant of solving the problem is very large (or some simulation title)

For the space, if the other room to open the array is unbearable

It indicates that this question is shaped pressure DP, and the object is compressed against a relatively small number of these

Well, this question of the state?

After defining $ $ DP one-dimensional array, wherein for $ dp_i $, $ I $ converted to binary, for which a bit, when this bit is one, indicating that in this state, the first $ I $ colors have all been put together a

, Then the number of two-dimensional array definition of $ c $, where $ c_ {ij} $ represents the front of all colors $ J $ $ I $ stones of the stone colors in the initial state

Then there shape binary transfer equation $$ dp_i = dp_ {i- (1 << j)} + cost (j) $$ wherein J $ $ $ I $ is the number of bits for a

Hu mouth still too difficult codes below

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define int long long
 4 #define cg (c=getchar())
 5 inline int qread(){
 6     int x,f=1;char c;
 7     while(cg<'0'||'9'<c)if(c=='-')f=-1;
 8     for(x=(c^48);'0'<=cg&&c<='9';x=(x<<1)+(x<<3)+(c^48));
 9     return x*f;
10 }
11 #undef cg
12 template<class T>inline T Max(const T x,const T y){return x>y?x:y;}
13 template<class T>inline T Min(const T x,const T y){return x<y?x:y;}
14 template<class T>inline T fab(const T x){return x>0?x:-x;}
15 const int MAXN=4e5;
16 const int INF=1ll<<60;
17 const int MAXA=25;
18 int a[MAXN+5],N,maxx;
19 int cnt[MAXA+5],c[MAXA+5][MAXA+5];
20 int dp[(1<<20)+5];
21 signed main(){
22     N=qread();
23     for(int i=1;i<=N;++i)maxx=Max(a[i]=qread(),maxx),--a[i];
24     for(int i=1;i<=N;++i){
25         ++cnt[a[i]];
26         for(int j=0;j<=maxx;++j)c[j][a[i]]+=cnt[j];
27     }
28     for(int i=0;i<=(1<<maxx)-1;++i)dp[i]=INF;
29     dp[0]=0;
30     for(int i=1;i<=(1<<maxx)-1;++i)for(int j=0;j<maxx;++j)if(i&(1<<j)){
31         int cost=0,pre=i-(1<<j);
32         for(int k=0;k<maxx;++k)if(pre&(1<<k))cost+=c[j][k];
33         dp[i]=Min(dp[i],dp[pre]+cost);
34     }
35     printf("%lld\n",dp[(1<<maxx)-1]);
36     return 0;
37 }
View Code

 

Guess you like

Origin www.cnblogs.com/MachineryCountry/p/11584175.html