Dynamic programming, dp

Dynamic classification regulations

  1. Linear actuator Regulation
  2. Interval moving Regulation
  3. Tree regulatory action

Regulation movable section
according to questions asked, the global optimum satisfy local optimum;

Exemplary embodiment title
plus a binary tree (Los Valley 1040)
entitled Introduction
subject description
is provided a sequence of n nodes of the binary tree traversal T (1,2,3, ..., n), where the number 2, 3, ..., n is the node number.
Each node has a score (both positive integers), record the score j-th node is dj. Score points binary T and each of its sub-tree has a plus, any subtree S (including T itself) plus equals left subtree S of points × right subtree of S + root of S . If a subtree is empty, the provisions of its plus 1 divided. Leaves scores extra points is the leaf node itself, regardless of its empty tree.
Determine a preorder conform to (1,2,3, ..., n) and the highest points of the binary tree T. T demanded output and the highest points preorder traversal.
Input format
Line 1: an integer n (n <30), is the number of nodes.
To line 2: n integers separated by spaces, each node as a fraction (fraction <100).
Output Format
Line 1: an integer, the highest points (result not exceed 4,000,000,000).
Line 2: n integers separated by spaces, for preorder traversal of the tree.

Sample input
. 5
. 5. 1. 7 2 10
Sample Output

145
3 1 2 4 5

Topic analysis
because it is in order traversal, and therefore never meet left subtree node number <root number <right child node number, although the tree, but the choice and decision-making is not a problem, and therefore not a tree rules;
the best in the global At the same time, meet the optimal range, so choose the interval dp;

Dynamic transfer equation
f [r, l] = max {f [i, k] * f [k, j] + a [i] [i]}

#include<cstdio>
#include<iostream>
using namespace std;
int a[20][20],r[20][20];//r数组存储区间根节点
int n;
void find(int x,int y)//前序遍历,dfs顺序 
{
    if(x<=y)
    {
        printf("%d",&r[x][y]);
        find(x,r[x][y]-1);
        find(r[x][y]+1,y);
    }
    return;
}
int main()//区间dp,局部最优满足全局最优,当前决策具有后效性 
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            a[i][j]=1;
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i][i]);
        r[i][i]=i;
    }
    for(int i=n;i>=1;i--)//a[i][j]枚举的是从i到J的顶点; 
        for(int j=1+i;j<=n;j++)//枚举区间首,尾
            for(int k=i;k<=j;k++)
            {
                if(a[i][j]<a[i][k-1]*a[k+1][j]+a[k][k])//同理,枚举区间i~k-1,k+1~j的最大值,再加上顶点自身值 
                {
                    a[i][j]=a[i][k-1]*a[k+1][j]+a[k][k];
                    r[i][j]=k;//将k定为当前区间根节点 
                }
            }
    printf("%d",a[1][n]);//输出的是从1~n的区间最大值; 
}

Linear actuator Regulation

A typical problem cases
missile interceptors, chorus formation;

The following is the longest rising sequence and the longest drop sequence template

#include<cstdio>
#include<iostream>
using namespace std;
int a[10001],b[10001],c[10001];

int main()
{
    int n=1;
    int maxn=0,mine=0;
    while(scanf("%d",&a[n])){n++;}n--;
        for(int i=1;i<=n;i++)
            scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)//最长上升
    {
        b[i]=1;
        for(int j=1;j<=i-1;j++)
            if((a[i]>=a[j])&&(b[j]+1>b[i]))b[i]=b[j]+1;
            if(b[i]>maxn)maxn=b[i];
    }
    for(int i=n;i>=1;i--)//最长下降
    {
        c[i]=1;
        for(int j=i+1;j<=n;j++)
        {
            if((a[i]<a[j])&&c[j]+1>c[i])c[i]=c[j]+1;
        }
        if(mine<c[i])mine=c[i];
    }
    printf("%d%d",maxn,mine);
    return 0;
}

Rise longest sequence of binary optimization

#include<cstdio>
#include<iostream>
using namespace std;
int n,top,a[100005],t;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&t);
        if(t>a[top])
        a[++top]=t;
        else
        {
            int low=1,high=top,mid;
            while(low<=high)
            {
                mid=(low+high)/2;
                if(a[mid]<t)
                low=mid+1;
                else
                high=mid-1;
            }
            a[low]=t;
        }
}
    printf("%d",top);
}

Tree regulatory action

No party boss

Topic Introduction
there is a company to hold a party.
In order to have a good time, company leaders decided: If you invite someone, then we will not invite his boss
(boss's boss, boss's boss's boss ...... can invite).

Everyone can add to the party atmosphere for the party number, seeking an invitation program, and the maximum value of the atmosphere.

INPUT :
Line 1 integer N (1 <= N <= 6000) represented by the number of companies.
Next N lines of an integer. I represents the number of the i-th row personal atmosphere value x (-128 <= x <= 127).
Next each row two integers L, K. K represents personal L is the first individual supervisor.
Enter at the end of 00.

the Output :
a number, maximum value and atmosphere.

input:
7
1
1
1
1
1
1
1
1 3
2 3
6 4
7 4
4 5
3 5
0 0

output:
5

Ideas analyzes
this question meet the characteristics of the tree, that is a directed acyclic, can have multiple successor, but only a precursor,
optimization problem, the current node select only about his son, meet no after-effect, you can choose a tree dp, thus achievements, DFS;

#include<iostream>
#include<cstdio> 
using namespace std;
int f[6001][2],father[6001];
bool v[6001];
int n;
int find_max(int a,int b)
{
    if (a>b) return a;
    else return b;
}
void dfs(int k)//f[i][0]储存不选用当前人的最大值,f[i][1]储存选用的最大值 
{
    v[k]=false;
    for (int i=1;i<=n;i++)
        if (v[i] && father[i]==k)
        {
            dfs(i);
            f[k][0]+=f[i][1];
            f[k][1]+=find_max(f[i][0],f[i][1]);
        }
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++) scanf("%d",&f[i][0]);
    int l,k,root;
    root=0;bool bk=true;
    while (scanf("%d%d",&l,&k),l+k>0)
    {
        father[l]=k;
        if (root==l || bk==true)//由于输入的无序性,因此必须找出根节点 
        {
            root=k;bk=false;//根节点等于当前人的上司 
        }
    }
    memset(v,true,sizeof(v));
    dfs(root);//从根节点开始搜索 
    printf("%d\n",find_max(f[root][0],f[root][1]));//输出决策是否选用第一个人(第一个人的数组已储存选与不选的最大值) 
    return 0;
}
Published 20 original articles · won praise 1 · views 6333

Guess you like

Origin blog.csdn.net/yichengchangan/article/details/60143789