HDU 3455 Leap Frog (linear DP)

Source: Internet
Author: User


Leap Frog Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 638 Accepted Submission (s): 224

problem DescriptionJack and Jill play a game called "Leap Frog" in which they alternate turns jumping all other. Both Jack and Jill can jump a maximum horizontal distance of ten units in any single jump. You is given a list of valid positions x1,x2,..., xn where Jack or Jill may stand. Jill initially starts at position x1, Jack initially starts at position x2, and their goal are to reach position xn. Determine the minimum number of jumps needed until either Jack or Jill reaches the goal. The players is never allowed to stand at the same position at the same time, the R must hop over the player in the front. InputThe input file would contain multiple test cases. Each test case would begin with a and a single line containing a single integer n (where 2 <= n <= 100000). The next line would contain a list of integers x1,x2,..., xn where 0 <=x1,x2,..., xn<= 1000000. The End-of-fi le is denoted by a single line containing "0".
 Outputfor each input test case, print the minimum total number of jumps needed for both players such that either Jack or Jil L reaches the destination, or-1 if neither can reach the destination. Sample Input
63 5 9 12 15 1763 5 9 12 30 40
 Sample Output
3-1
 SourceStanford Local ACM programming Contest
Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3455

The main idea: N sites in a line, the position of Xi, two frogs, the first start in the first site, the second only start at the second site, they jump up to 10 units each time, and each hop must exceed each other (cannot fall on the same site), Two frogs the minimum value of the total number of hops to reach the destination that is the last site

Topic Analysis: This problem do special tangle, dp[i][0],dp[i][1], respectively, said two frog jump I time the furthest distance, jumping when to do the following judgment:
1. Current frog can exceed the front frog
2. Can fall to a site (note that there is more than the end of the situation)
3. A frog behind the frog can overtake it and fall on a site.
To meet the above conditions to take the maximum value, we can know if the first to go and jump I times, the total number of 2*i-1, if the second is 2*i

#include <cstdio> #include <cstring> #include <algorithm>using namespace std;int const INF = 0x3fffffff    ; int const MAX = 100005;int dp[max][2], x[max];bool Has[max * 10];int main () {int n;        while (scanf ("%d", &n)! = EOF && n) {int ans =-1;        BOOL flag = FALSE;        Memset (has, false, sizeof (have));        memset (DP, 0, sizeof (DP));            for (int i = 0; i < n; i++) {scanf ("%d", &x[i]);        Has[x[i]] = true;            } if (n = = 2) {printf ("0\n");        Continue        } Dp[0][0] = x[0];        DP[0][1] = x[1]; for (int i = 1, i < n; i++) {for (int j = 2; J <=; J + +) {int D0 = dp[                I-1][0] + j; if ((has[d0] | | d0 >= x[n-1]) && d0 > Dp[i-1][1]) {for (int k = 2; k &L t;= 10; k++) {if (dp[i-1][1] + k > D0 && (Has[dp[i-1][1] + K] | | dp[i-1][1] + k >= x[n-1]) {                            Dp[i][0] = max (dp[i][0], d0);                        Break                }}}} for (int j = 2; J <=; J + +) {                int D1 = dp[i-1][1] + j; if ((Has[d1] | | D1 >= x[n-1]) && d1 > Dp[i][0]) {for (int k = 2; k <= 10; k++) {if (dp[i][0] + k > D1 && (has[dp[i][0] + K] | | dp[i][0] + k                            (= X[n-1])) {dp[i][1] = max (dp[i][1], D1);                        Break }}}}} for (int i = 1; i < n; i++) {if (                Dp[i][0] >= x[n-1]) {flag = true; printf ("%d\n", 2 *I-1);            Break                } if (Dp[i][1] >= x[n-1]) {flag = true;                printf ("%d\n", 2 * i);            Break    }} if (!flag) printf (" -1\n"); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3455 Leap Frog (linear 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.