2016 Program Design Practice Final Examination 07 Questions on machine (DP) __algorithm

Source: Internet
Author: User

2016 Program Design Practice Final Examination 07 Questions on machine (DP)
Total time limit: 1000ms memory limit: 65536kB

Describe
Again to the weekend, the students continued to open a happy heart to the machine room. JBR is no exception, but he arrived a little late, found that some of the seats have been students are doing problems, some seats are still empty. Careful jbr found that a classmate came to the computer room, sitting on the seat I, if his left and right sides are empty, he will gain the ability to value a[i]; if he sits down, there is a man on the left or right who is on the machine, he will get the ability value b[i]; if he sits down, there's someone on the right side of the machine, He will get the ability to value c[i].

At the same time, he found that the students who have been on the machine will not be affected by the students just to sit down, that is, the value of their ability will be only when sitting down, will not change; the first one has no seat on the left and the last one is not on the right, and no C value can be obtained whenever you sit on the two seats.

At this time JBR found a row of machines are still empty, a total of n seats, numbered 1 to N. At this time there are N students continue to come to the computer room, a one in order to sit on this row of seats. Smart JBR want to know how to arrange the order of the seats, you can make this n students get the value of the maximum and the ability.

Input
First line an integer n (1<= n <= 10000)

Second row n number, representing A[i]

Third row n number, representing B[i]

The number of n in line four, representing C[i]

(1<= A[i],b[i],c[i] <=10000)

Output

An integer that represents the maximum possible value for the ability and

Sample input

4
1 2 2 4
4 3 3 1
2 1 1 2

Sample output

14

Tips
The first student sits on the fourth seat, obtains the ability value 4;
The second student sits on the third seat, obtains the ability value 3;
The third student sits on the second seat, obtains the ability value 3;
The fourth student sits on the first seat, obtains the ability value 4;
The sum is 14.

The exam did not think out, then think of the beginning of the wrong.
What is the state of nature behind the complicated sequence?
I first thought it was a thing. The number of adjacent empty spaces, and it is easy to prove that the sum of adjacent empty numbers is n+1 as a sufficient and necessary condition for existence of such order. The need to consider in addition to the outermost two vacancies outside, the other (n-1) on the adjacent vacancy is bound to get the number of vacancies, note that there is a total vacancy (n-1) +2=n+1, certificate. Adequacy, can be summed up to prove. First of all there must be 2, will be left into the last 2, if the rest is all 1, in turn, if the remaining 0, it is inevitable that the left side of the adjacent right lattice a, will be the right side of the grid after the completion (by inductive assumption this to do), and a put a 0, also can, the certificate.
With this conclusion, it is equivalent to calculate the maximum value of the n+1 cost, which is assigned to the cost 0,1,2. Become a knapsack problem, notice some special details just fine, but unfortunately the complexity is O (n2) O (n^2), TLE, this is version 1.
Later I read the code of Whz and studied version 2. Just note that the number of n before the grid is finished (instead of the first n put in the lattice), the number of n squares before the value of the grid only with the n time and the empty and the first number n-1 a lattice score. So you only need to maintain a sequence dp[i][t1][t2], where t1,t2 is 0 when the left right is empty, and 1 means the left right is full. Version 2 does not look like version 1 to consider the order of placement but consider the position order, changing an angle, the state is relatively simple, and then reduced the dimension, do O (n) o (n), want to wonderful.

Version 1:

Wrong Answer    2   520kB   1730ms  546 B   g++
#define MAX_N 10000

#include <stdio.h>

int a[max_n+1][3];
int f[max_n+4];
int n;

void Test ()
{/
    *
    for (int i=0;i<=n+1;i++)
        printf ("%d  ", F[i]);
    printf ("\ n");
    return;
    */
} 

int main ()
{
    scanf ("%d", &n);
    for (int j=2;j>=0;j--)
        for (int i=1;i<=n;i++)
            scanf ("%d", &a[i][j]);
    f[0]=0;
    for (int i=1;i<n+2;i++)
        f[i]=0x80000000;
    for (int i=1;i<=n;i++) {for (int
        t=n+1;t>=0;t--) for
            (int j= (i==1) | | (i==n)); j<=2 && j<=t;j++)
                if (F[t]<f[t-j]+a[i][j])
                    f[t]=f[t-j]+a[i][j];
        Test ();
    }
    printf ("%d\n", f[n+1]);
    return 0;
}

Version 1:

Accepted  524kB   0ms 604 B   g++
 #define MAX_N 100000 #include <stdio.h> int N; int v[max_n][3]; int dp[max_n][2][2];

inline int Max (int a,int b) {return a>b?a:b;}
    int main () {scanf ("%d", &n);
    for (int i=0;i<=2;i++) for (int j=0;j<n;j++) scanf ("%d", &v[j][i]); for (int i=0;i<n;i++) for (int j=0;j<2;j++) for (int k=0;k<2;k++) dp[i][j][k]
    =0x80000000;
    DP[0][0][0]=V[0][0];
    DP[0][0][1]=V[0][1]; for (int i=1;i<n;i++) for (int j=0;j<2;j++) for (int k=0;k<2;k++) for (int t= 
    0;t<2;t++) Dp[i][k][t]=max (dp[i][k][t],dp[i-1][j][k^1]+v[i][k+t]);
    printf ("%d", Max (dp[n-1][0][0],dp[n-1][1][0));
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.