UVA-1632 Alibaba (interval dp+ scrolling array)

Source: Internet
Author: User

Title: There are n pieces of jewelry in a straight line, the location of each piece of jewellery is known, and the first piece of jewelry disappears at ti moment, q can I collect all the jewels? If you can, find the shortest time. The collection can be done instantaneously.

Topic Analysis: Interval DP. DP (i,j,0) indicates the minimum time required to collect the interval (i,j) and stay at the left, and DP (i,j,1) represents the minimum time required to collect the interval (i,j) and stay on the right side. The state transition equation is

DP (i,j,0) =min (DP (i+1,j,0) +t (i+1)-T (i), DP (I+1,J1,) +t (j)-T (i)), DP (i,j,1) =min (DP (i,j-1,0) +t (j)-T (i), DP (i,j-1,1) +t (j)-T (j-1)).

The data size of the problem is large, you can use the scrolling array to optimize the spatial complexity.

The code is as follows:

# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace        Std;int dp[2][10005][2];int x[10005],t[10005],n;const int inf=0x7fffffff;int main () {while (~SCANF ("%d", &n)) {            for (int i=0;i<n;++i) {scanf ("%d%d", x+i,t+i);        dp[1][i][0]=dp[1][i][1]=dp[0][i][0]=dp[0][i][1]= (t[i]>0) 0:inf; } for (int i=n-2;i>=0;--i) {for (int j=i+1;j<n;++j) {Dp[i&1][j][0]=dp[i&1][j]                [1]=inf; if (dp[(i&1) ^1][j][0]!=inf&&dp[(i&1) ^1][j][0]+x[i+1]-x[i]<t[i]) dp[i&1][j][0]=min                (dp[i&1][j][0],dp[(i&1) ^1][j][0]+x[i+1]-x[i]); if (dp[(i&1) ^1][j][1]!=inf&&dp[(i&1) ^1][j][1]+x[j]-x[i]<t[i]) Dp[i&1][j][0]=min (d                p[i&1][j][0],dp[(i&1) ^1][j][1]+x[j]-x[i]);              if (Dp[i&1][j-1][0]!=inf&&dp[i&1][j-1][0]+x[j]-x[i]<t[j])      Dp[i&1][j][1]=min (Dp[i&1][j][1],dp[i&1][j-1][0]+x[j]-x[i]); if (Dp[i&1][j-1][1]!=inf&&dp[i&1][j-1][1]+x[j]-x[j-1]<t[j]) dp[i&1][j][1]=min (dp[            I&1][j][1],dp[i&1][j-1][1]+x[j]-x[j-1]);        }} if (Dp[0][n-1][1]==inf&&dp[0][n-1][0]==inf) printf ("No solution\n");    else printf ("%d\n", Min (dp[0][n-1][0],dp[0][n-1][1])); } return 0;}

  

  

The following is a non-scrolling array:

# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace Std;int dp[10005][10005][2];int x[10005],t[10005],n;const int inf=1000000000;int main () {    while (~scanf ("%d", &n))    {        for (int i=0;i<n;++i)            scanf ("%d%d", x+i,t+i), dp[i][i][0]=dp[i][i][1]=0;        for (int i=n-2;i>=0;--i) {for            (int j=i+1;j<n;++j) {                dp[i][j][0]=min (dp[i+1][j][0]+x[i+1]-x[i],dp[i+1] [j] [1]+x[j]-x[i]);                if (Dp[i][j][0]>=t[i]) Dp[i][j][0]=inf;                Dp[i][j][1]=min (Dp[i][j-1][0]+x[j]-x[i],dp[i][j-1][1]+x[j]-x[j-1]);                if (Dp[i][j][1]>=t[j]) dp[i][j][1]=inf;            }        }        if (dp[0][n-1][1]==inf&&dp[0][n-1][0]==inf) printf ("No solution\n");        else printf ("%d\n", Min (dp[0][n-1][0],dp[0][n-1][1]));    }    return 0;}

  

UVA-1632 Alibaba (interval dp+ scrolling array)

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.