HDU 3455 Leap Frog (线性dp)
来源:互联网 发布:篮球鞋淘宝店 编辑:程序博客网 时间:2024/04/30 09:21
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 Description
Jack 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.
The input file will contain 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".
For 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.
63 5 9 12 15 1763 5 9 12 30 40
3-1
2009 Stanford Local ACM Programming Contest
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3455
题目大意:n个站点在一条线上,位置为xi,两只青蛙,第一只开始在第一个站点,第二只开始在第二个站点,它们每次最多跳10个单位长度,且每次跳必须超过彼此(不能落在同一个站点上),求两蛙任意一个到达目的地即最后一个站点时跳跃的总次数的最小值
题目分析:这题做的特别纠结,dp[i][0],dp[i][1],分别表示两青蛙跳i次的最远距离,跳的时候要做以下判断:
1.当前青蛙能超过前面的青蛙
2.超过后能落到某个站点(注意这里还要注意超过终点的情况)
3.超过后该青蛙的后面一只青蛙也能够超过它且落在某个站点上
满足以上条件的情况下取最大值,可知若第一只先到且跳了i次,总次数为2*i-1,若第二只则为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(has)); 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 <= 10; 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 <= 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 <= 10; 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"); }}
0 0
- HDU 3455 Leap Frog(线性DP)
- HDU 3455 Leap Frog (线性dp)
- hdu 3455 Leap Frog
- HDU 3455 Leap Frog
- hdu 2128 Frog(简单DP)
- hdu 2182 Frog【Dp】水题
- THE LEAP-FROG 跳高者
- HDU 1003 (线性dp)
- HDU 1421(线性DP)
- hdu 5555 Immortality of Frog(状压dp)
- HDU 4433 locker (线性dp)
- HDU 4293 Groups (线性dp)
- HDU-2546 饭卡【线性dp】
- HDU 1260 Tickets【线性dp】
- HDU-2571 命运 (线性dp)
- hdu 2182 Frog
- hdu 5037 Frog
- hdu 5037 Frog(贪心)
- 每天一个linux命令(56):netstat命令
- 【算法】Bloom Filter
- 数据库索引失效
- 每天一个linux命令(57):ss命令
- 第十三周
- HDU 3455 Leap Frog (线性dp)
- Java路径操作详解
- jvm文章
- javascript深入理解js闭包
- HDU 1012 u Calculate e
- 湖北天门特色面食麻酱烧饼盛亿隆
- 第十四周
- 每天一个linux命令(58):telnet命令
- yeelink平台试玩