题目来源
https://ac.nowcoder.com/acm/contest/317/C
题意
给定一个长度为
的序列,没一个值表示第
个星球的能量指数为
,飞船当前耐力指数为第一个星球的能量指数,当星球
能到达星球
当且仅当
,假设当前飞船的耐久值为
,当飞船到达星球
时船的耐久值会变为
。(
)
现在求飞船到达第
个星球的最大耐久值,到达不了或者耐久值为
都输出
。
思路
显然,此题就是判断当前状态是否可达
,那么
即可。
ps:去年比赛的时候是用的sb 01背包写的,数据水了。
参考代码
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int v[3005], book[10000];
queue<int> q;
int main() {
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &v[i]);
}
book[v[1]] = 1;
q.push(v[1]);
int ans = 0;
while (!q.empty()) {
int now = q.front(), nxt;
q.pop();
for (int i = 2; i <= n; ++i) {
if (now > v[i]) {
nxt = now ^ v[i];
if (i == n && nxt > ans) {
ans = nxt;
}
if (i != n && book[nxt] == 0) {
q.push(nxt);
book[nxt] = 1;
}
}
}
}
if (ans == 0) {
printf("-1\n");
} else {
printf("%d\n", ans);
}
return 0;
}
``