2019牛客寒假算法基础集训营1C

题目来源

https://ac.nowcoder.com/acm/contest/317/C

题意

给定一个长度为 n n 的序列,没一个值表示第 i i 个星球的能量指数为 p i p_i ,飞船当前耐力指数为第一个星球的能量指数,当星球 i i 能到达星球 j j 当且仅当 p i > p j p_i > p_j ,假设当前飞船的耐久值为 t t ,当飞船到达星球 j j 时船的耐久值会变为 t p j t \oplus p_j 。( 1 n , p i 3000 1\leq n,∀p_i \leq 3000
​现在求飞船到达第 n n 个星球的最大耐久值,到达不了或者耐久值为 0 0 都输出 1 -1

思路

显然,此题就是判断当前状态是否可达 n n ,那么 b f s bfs 即可。
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;
}
``
发布了311 篇原创文章 · 获赞 226 · 访问量 13万+

猜你喜欢

转载自blog.csdn.net/nuoyanli/article/details/104165852