2019 summer training DAY15 (problem3.Miku) (pressure state)

(This question is, in fact, changed the title bzoj2046 face)

Love across time and space

Time Limit:1Sec Memory Limit:64MB Miku.cpp/in/out

Description

Thus she is gone, along with cherry blossoms drift, so she left.

Dull turned out to be the greatest feeling of grace. Moment in million light-years from the chance to see her in another dimension, but were in the flat with her fleeting bit of cherry trees in the yard. Knows no boundaries time and space wilderness, fell in love in the spring season, she come with cherry, with cherry and go. the second element of the unexpected emergence of the storm, took three dollars she strayed. yard cherry also swept drift in the air separation polymerization. too late to get along with a brief sigh, OY eyes only residual mourning over the floor. everything is gone with the wind yet? OY seem to recall when it looks like cherry trees first met each flower petal. If you try to recover, she will the same time the cherry trees, like as then miraculously come of it in the tree?

N1 past cherry trees have a flower cluster, the number of flowers per inflorescence OY also remember when the storm swept through the second element, repeatedly changed its appearance cherry tree. Each change or to blow open into a flower cluster two clusters, or is it the two rope became a cluster of flower clusters blow. Although the total amount of flowers the same, but the number after sweeping the flower clusters and the number of flowers per inflorescence have changed.

OY cherry trees now need to operate the same class dimension to the storm recovery. If a flower cluster into two clusters, two or gather into a flower cluster flower cluster treated as two operations, OY want as little as possible the number of operations in the recovery cherry trees.

No one can predict the fate of the trend, if you do not choose to do something, then.

Input

The first line of a number n1, the number of flower clusters represent the former, then the number n1 represent the number of flower clusters of flowers each.

The second line a number n2, indicates that the current number of flower clusters, the next number n2 represent the number of flower clusters of flowers each.

Output

The minimum number of output

Sample Input

1 6

3 2 3 1

Sample Output

2

Sample Explanation

1 and 2 will operate once gathered to 3, then 3 and 3 to 6 a gather operation, a total of two operations.

data range

For the first 30% of the data, n1, n2 <= 6.

To 100% of the data, n1, n2 <= 10, each number <= 50.


Very clear idea of the odd-shaped pressure, anyway, not that I can think of == ( reference )

analysis:

Consider worst-case operation, the sequence of all the need to gather operating sequence is subdivided into n1 + n2-2 times

If we choose an initial selection k0 and the k is exactly equal to the target state and can separate them by k0 + k-2 operations reaches the target state,

By remaining n1-k0 + n2-k-2 operations target state is reached, a total operating time n1 + n2-4

Decrease compared to the original twice.

In other words, if you can find more such blocks, the more we reduce the number of operations .

So consider like pressure, dp [i] i represents the state number of such blocks can be found, i 1 can be reduced to any one of the transition from state i.

Can n1, n2 combined into one sequence, and 1 and 0 indicates no option is selected, and if the sum is the sum of 0, indicating a block found to meet the conditions.

The final answer is n-dp [1 << (n) -1]

#include<bits/stdc++.h>
#define LL long long
#define INF 2100000000
using namespace std;
int read()
{
    int x=0,f=1;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    return x*f;
}
int dp[1<<21],a[22],sum[1<<21];
int main()
{
    int n=read();
    for(int i=1;i<=n;++i)a[i]=read();
    int m=read();
    for(int i=1;i<=m;++i)
    {
        a[i+n]=read();
        a[i+n]=-a[i+n];
    }
    n+=m;
    for(int i=1;i<(1<<n);++i)
    {
        for(int j=1;j<=n;++j)
            if(i&(1<<j))dp[i]=max(dp[i],dp[i^(1<<j)]),sum[i]+=a[j];
          if(sum[i]==0)dp[i]++;
    }
    printf("%d\n",n-2*dp[(1<<n)-1]);
    
} 
View Code

Guess you like

Origin www.cnblogs.com/yyys-/p/11267816.html