DP sets of questions Exercise 2

T1: missile interceptors

S1:. Q1 does not rise to the longest sequence, Q2 for the sake of increased longest sequence is monotonic employed Tacca stack approach to take demand rises longest sub-sequences, the a [1] to the stack, and then maintaining monotonicity rising sequence, if greater than q [lenth], the stack, if not greater than the binary search, to find a ratio of the first stack monotone large number of its place, although not rigorous proof found, continue to be appreciated that the inductive instead of a large number of process, so that the maximum value of the stack monotonous to the development of small, a greater number of inbound, that is the answer .Q2 lenth uses Dilworth theorems : minimum = the longest chain of the anti-dividing chain length.

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define e exit(0)
#define R register
int n,len,h[100010],q[100010];
int main()
{
    freopen("missile.in","r",stdin);
    freopen("missile.out","w",stdout);
    scanf("%d",&n);
    for(R int i=1;i<=n;++i)
        scanf("%d",&h[i]);
    q[++len]=h[n];
    for(R int i=n-1;i>=1;--i){
        if(h[i]>=q[len])
            q[++len]=h[i];
        else{
            int id=lower_bound(q+1,q+1+len,h[i])-q;
            q[id]=h[i];
        }
    }
    printf("%d\n",len);
    memset(q,0,sizeof(q));
    len=0;
    q[++len]=h[1];
    for(R int i=2;i<=n;++i){
        if(h[i]>=q[len])
            q[++len]=h[i];
        else{
            int id=lower_bound(q+1,q+1+len,h[i])-q;
            q[id]=h[i];
        }
    }
    printf("%d",len);
    return 0;
}

T2: integer division

S2: we can see that we have provided an interval DP F [i] [j] denotes the front i j has a number of product at the maximum multiplication j∈ [0, i-1] of the divided position of the point k. enumeration may start from j (i.e., j-th multiplication symbol after the first number j) .f [i] [j] = max {f [k] [j - 1] + getv (k +1, i) } .getv function that is required size range of this number. but there are questions, we find how divided, split point record, with an array of precursors to go back to.

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
#define e exit(0)
#define R register
#define ll long long
long long T,n,m,ans,lenth,deep,num[110],f[110][110],q[110],lastr[110][110][3];
void cf(long long x)
{
    deep=0;
    long long len=0,mix[110];
    while(x)
    {    
        mix[++len]=x%10;
        x/=10;
    }
    for(R int i=len;i>=1;--i)
        num[++deep]=mix[i];
}
long long getv(ll x,ll y)
{
    long long sum=0;
    for(R ll i=x;i<=y;++i)
        sum=sum*10+num[i];
    return sum;
}
int main()
{
    freopen("separate.in","r",stdin);
    freopen("separate.out","w",stdout);
    scanf("%lld",&T);
    while(T--){
        lenth=0;
        memset(f,0,sizeof(f));
        memset(q,0,sizeof(q));
        memset(num,0,sizeof(num));
        memset(lastr,-1,sizeof(lastr));
        scanf("%lld%lld",&n,&m);
        cf(n);
        for(R int i=1;i<=deep;++i)
            f[i][0]=getv(1,i);
        for(R int i=1;i<=deep;++i)
            for(R int j=1;j<=deep-1;++j)
                for(R int k=j;k<=i-1;++k)
                    f[i][j]=max(f[i][j],f[k][j-1]*getv(k+1,i));
        for(R int i=1;i<=deep;++i)
            for(R int j=1;j<=deep-1;++j)
                for(R int k=j;k<=i-1;++k)
                    if(f[i][j]==f[k][j-1]*getv(k+1,i)){
                        lastr[i][j][0]=k;
                        lastr[i][j][1]=j-1;
                        lastr[i][j][2]=f[k][j-1];
                    }
        printf("%lld\n",f[deep][m-1]);
        if(f[deep][m-1]==0){
            for(R int i=1;i<=deep;++i)
                printf("%lld ",num[i]);
            printf("\n");
            continue;
        }
        long long lax=deep,lay=m-1;
        while(lax!=-1&&lay!=-1){
            if(lastr[lax][lay][2]!=-1)
                q[++lenth]=lastr[lax][lay][2];
            ll nowx=lastr[lax][lay][0],nowy=lastr[lax][lay][1];
            lax=nowx,lay=nowy;
        }
        long long num=f[deep][m-1]/q[1];
        for(R int i=1;i<=lenth-1;++i)
            q[i]=q[i]/q[i+1];
        for(R int i=lenth;i>=1;--i)
            printf("%lld ",q[i]);
        printf("%lld\n",num);
    }
    return 0;
}

T3: Peter R City recently opened a fast food restaurant, in order to attract customers, the fast-food restaurant ready to launch a package, the package of hamburger A, B and C fries a beverage composition in order to improve productivity, Peter from famous. McDonald's introduced the N production lines. All production lines can produce burgers, fries and drinks, as each production line to offer daily production time is limited, different, and burgers, fries and drinks unit production time is different. this makes it difficult for Peter, I do not know how to make arrangements for the production of packages produced maximum yield of the day you please compile a program that calculates the maximum production capacity of day packages. For simplicity, assume burgers, fries and drinks per day not more than 100.

S3: no idea, only f [i] [A] [ B] [C] of the four-dimensional DP, time and space are clearly seen through the blow solution to a problem, found that the number of packages A, B, C can be composed of the minimum. the number of packages. we set f [k] [i] [ j] represent the first k selected i-a, but also choose the maximum number of C. here is an enumeration of optimization when the j-th B, we determined the number of sets of upper limit maxn = min (100 / a, min (100 / b, 100 / c)), then the number is the upper limit to enumerate or maxn * a maxn * b. cooing.

Guess you like

Origin www.cnblogs.com/xqysckt/p/11391567.html