UVA 1632-alibaba (DP)

Source: Internet
Author: User

The main idea: in a straight line there are N (n<=10000) points have treasures, each point coordinates are integers, each treasure in a certain time to disappear after, you can start at any point, moving a unit needs a unit of time, ask at least how much time to take out all the treasures, If you cannot take out all the output no solution. The input is incremented in order of coordinates.


A[i] denotes the location of the first treasure, B[i] indicates the time of disappearance.

First notice that for any interval (i,j), after taking all the treasures must be in either I or J. So the d[i][j][0] means to take all the treasures between the first and the first J, and at the end of I, using d[i][j][1] to represent all the treasures between the first and the first J and the last in J.

The program uses a scrolling array to optimize memory consumption, and recursion is in order of increasing the width of the interval, so it is also scrolling in this way.


State transition equation:

d[i][j][0]=min {D[i+1][j][0]+a[i+1]-a[i],d[i+1][j][1]+a[i+j]-a[i]}

d[i][j][1]=min {D[i][j-1][0]+a[i+j]-a[i],d[i][j-1][1]+a[i+j]-a[i+j-1]}

In the program, you also need to determine whether the recursive process can be reached before disappearing, and will be set to INF.


#include <stdio.h> #include <stdlib.h>int a[10010];int b[10010];int d[2][10010][2];int Main (void) {int i,j, N,cur,min;while (scanf ("%d", &n) ==1) {for (i=1;i<=n;i++) {scanf ("%d%d", &a[i],&b[i]);} Cur=0;for (j=1;j<=n;j++) {d[0][j][0]=d[0][j][1]= (b[j]>0)? 0: (1<<30);} for (i=1;i<n;i++) {cur^=1;for (j=1;j<=n-i;j++) {d[cur][j][0]=d[cur^1][j+1][0]+a[j+1]-a[j]>d[cur^1][j+1][1 ]+A[J+I]-A[J]?D[CUR^1][J+1][1]+A[J+I]-A[J]:d [cur^1][j+1][0]+a[j+1]-a[j];d [cur][j][1]=d[cur^1][j][0]+a[j+i]-a[j ]>D[CUR^1][J][1]+A[J+I]-A[J+I-1]?D[CUR^1][J][1]+A[J+I]-A[J+I-1]:d [cur^1][j][0]+a[j+i]-a[j];d [cur][j][0]=d[ CUR][J][0]<B[J]?D[CUR][J][0]:(1<<30);d [cur][j][1]=d[cur][j][1]<b[j+i]?d[cur][j][1]:(1<<30);}} MIN=D[CUR][1][0]>D[CUR][1][1]?D[CUR][1][1]:d [Cur][1][0];if (min== (1<<30)) {printf ("No solution\n");} else{printf ("%d\n", Min);}} return 0;}


UVA 1632-alibaba (DP)

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.