[Game] 9.1

1> transformation binary tree

A tree, modify the number of the above, the number of binary search trees become

That preorder strictly increasing

 

First, sorting is a binary tree, left his son the right value strictly less than the father, the son of the right weight strictly greater than the father of a binary tree (BST).

The so-called "sorting" is sort of binary tree traversal sequence obtained is a strictly increasing sequence.

So clearly we must first preorder trip, to construct arrays A,

Then it becomes the subject seeking to make changes to a strictly increasing sequence, the minimum required number of modifications

In order not to modify our point of view violence which transformed thinking, look at what point can leave

Here the remaining points, Aj-Ai> = j-i,

This strange ah further conversion:

The a [i] -i then find the longest sequence can nondecreasing 

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
int n;
const int N=1e5+3;
int d[N],son[N][2];
int a[N],cnt;
int up[N];

void mid_dfs(int pos)
{
    if(son[pos][0]) mid_dfs(son[pos][0]);
    a[++cnt]=d[pos]-cnt;
    if(son[pos][1]) mid_dfs(son[pos][1]);
}

int main () 
{ 
    Scanf ( " % D " , & n-);
     for ( int I = . 1 ; I <= n-; I ++) Scanf ( " % D " , & D [I]);
     int F, PS;
     for ( int I = 2 ; I <= n-; I ++ ) 
    { 
        Scanf ( " % D% D " , & F, & PS); 
        Son [F] [PS] = I; 
    } 
    mid_dfs ( . 1 ); // simple step, title actually completely changed appearance 
    
    int sz = 1 ;
    up [ . 1 ] = A [ . 1 ];
     for ( int I = 2 ; I <= CNT; I ++ ) 
    { 
        int POS = upper_bound, (up + . 1 , up + SZ + . 1 , A [I]) - up; // ask from in descending order, the write upper, represented == established when> is modified 
         IF (POS> SZ) SZ ++ ; 
        up [POS] = a [I]; 
    } 
    
    the printf ( " % D \ n- " , N- SZ); 
    
    return  0 ; 
}

2> Comparative switching elements two schemes

Switching elements, so that a sequence of ordered, the number of exchanges required minimum

The first is the only exchange "adjacent" element , so that an orderly sequence, find the minimum number of exchanges,

If it is a sequence of ascending, only find the number of reverse .

3 2 1-> 1 2 3 

3 times

4 2 3 1 -> 1 2 3 4

2 3 1 4 (3)

2 1 3 4 (1)

1 2 3 4 (1)

Each number corresponds exactly, and the number of the right to reverse his number

 

The second is to exchange positions of any two elements, so ordered, the minimum required number of exchanges,

The answer is: the number of rings (substituted ring) n- exchange of digital form .

(That is, all the rings of each data exchange, and the number)

For example {51324768}, the sequence will find the minimum number of times into ascending sequence exchange,

Then the ring has the sequence {5,1,2,4}, {7,6}, {3}, {8}, then the minimum number of exchanges is 8-4,

 

The minimum number of exchanges seeking descending order, simply reverse the sequence set, and then solve it.

 

3> exchange

Copy From: https://www.cnblogs.com/Damitu/p/7646694.html

Really tired, I do not want to fight

Problem solution:
      ① down to think a lot of trouble, consider the reverse: the last exchange position occurs

      ② After upside down, memory search, enumerate the current state of the exchange takes place in what position, and then divided into two sections deal with sub-issues

      ③ Note: Location is the exchange may take place in two parts left and right it should contain some number (out of order can be)

      ④ Transfer equation: F [L] [R & lt] + = the DFS (L, I) the DFS * (. 1 + I, R & lt) * C [-rl is an. 1] [IL]

            Indicates that the current interval [l, r], exchange i, i + 1, since independent decisions on both sides, there is a combination relation, can be transformed into the discharge space has a number 2n of program number n, this is because each side of the interior orderly (this is an important conclusion).

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
int n;
const int N=53,mod=1e9+7;
int st[N],ed[N];

int c[N][N];
void prepare()
{
    c[0][0]=1;
    for(int i=1;i<=n;i++)
    {
        c[i][0]=1;
        for( Int J = . 1 ; J <= I; J ++ ) 
            C [I] [J] = (C [I- . 1 ] [J] + C [I- . 1 ] [J- . 1 ])% MOD; 
    } 
} 

int ANS;
 Long  Long JL [N] [N];
 Long  Long DFS ( int L, int R & lt) 
{ 
    IF (JL [L] [R & lt]> = 0 ) return JL [L] [R & lt]; // magic plus memory of the 
    IF (R & lt == L) return JL [L] [R & lt] = . 1 ; 
    
    JL [L] [R & lt] = 0 ;
    for(int i=l;i<r;i++)
    {
        swap(st[i],st[i+1]);
        
        int j,k;
        for(j=l;j<=i;j++)
            if(st[j]>i) break;
        for(k=i+1;k<=r;k++)
            if(st[k]<=i) break;
        
        if(j>i && k>r)
        {
            long long ans1=dfs(l,i)*dfs(i+1,r)%mod;
            long long ans2=ans1*c[r-l-1][i-l]%mod;
            jl[l][r]=(jl[l][r]+ans2)%mod;
        }
        
        swap(st[i],st[i+1]);
    }
    return jl[l][r];
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d",&st[i]),ed[i]=i;
    
    prepare();
    memset(jl,-1,sizeof(jl));
    dfs(0,n-1);
    printf("%lld\n",jl[0][n-1]);
    
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/xwww666666/p/11444379.html
9.1