HDU 3455 leap frog (linear DP)

Source: Internet
Author: User
Problem descriptionjack and Jill play a game called "leap frog" in which they alternate turns jumping over each other. both Jack and Jill can jump a maximum horizontal distance of 10 units in any single jump. you are 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 is to reach position XN. determine the minimum number of jumps needed until either Jack or Jill reaches the goal. the two players are never allowed to stand at the same position at the same time, and for each jump, the player in the rear must hop over the player in the front.
Inputthe input file will contain in multiple test cases. each test case will begin with a single line containing a single integer N (where 2 <= n <= 100000 ). the next line will 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 Jill 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 indicates the minimum step from the first person to the second person J by DP [I] [J. DP [I] [J] = min {DP [J] [k] + 1 }.
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <limits. h> typedef long ll; using namespace STD; const int INF = 0x3f3f3f; const int maxn = 1e5 + 100; int DP [maxn] [15]; int hash [10 * maxn]; int num [maxn], n; int main () {While (~ Scanf ("% d", & N) {memset (DP, INF, sizeof (DP); memset (hash,-1, sizeof (hash )); for (INT I = 1; I <= N; I ++) {scanf ("% d", & num [I]); hash [num [I] = I;} DP [2] [num [2]-num [1] = 0; For (INT I = 2; I <= N; I ++) {for (Int J = 1; j <= 10; j ++) {If (j> num [I]) break; for (int K = J + 1; k <= 10; k ++) {int TT = num [I]-J; If (hash [TT]> = 0) DP [I] [J] = min (DP [hash [TT] [k-J] + 1, DP [I] [J]); // recurrence} int ans = inf; For (INT I = 1; I <= 10; I ++) // {// cout <"1111" <DP [N] [I] <Endl; ans = min (ANS, DP [N] [I]); //} If (ANS <inf) printf ("% d \ n", ANS); else printf ("-1 \ n");} return 0 ;}


 

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.