您的位置:首页 > 其它

计蒜客 跳跃游戏 贪心

2017-05-10 22:30 239 查看
           思路:假设当前位于pos,这个点能跳跃的最大距离是len[pos],那么下一个点应该是next_pos = j满足max{j + len[j]},即下一个点要能到达最远的距离。例如:2 0 2 0 1,当pos=0,它能到达1 和 2,2 + len[2] > 1 + len[1],所以选择下一个点是2而不是1.

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <bitset>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 500 + 5;
int a[maxn];
int main() {
int n;
while(scanf("%d", &n) == 1) {
for(int i = 0; i < n; ++i) {
scanf("%d", &a[i]);
}
int pos = 0;
while(a[pos]) {
if(pos == n-1) {
break;
}
int jump = a[pos];
int next_pos = pos+1;
for(int i = pos+1; i <= pos+jump && i < n; ++i) {
if(a[i] + i >= next_pos + a[next_pos]) next_pos = i;
}
pos = next_pos;
}
if(pos == n-1) printf("true\n");
else printf("false\n");
}
return 0;
}

如有不当之处欢迎指出!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: