Codefroces 1033C. Permutation Game

简介: 笔记

C. Permutation Game


题意:

一个线性的棋盘,上面有n个格子编号为1-n,当棋子所在位置满足以下情况时,可以移动

1.新格子的数值必须严格大于旧格子

2.移动的距离必须是旧格子中数字的整数倍

谁不能采取行动,谁就输了,即当前棋子位于一个不能移动的位置

求哪些出发格子可以使Alice必胜


思路

记录每个点下一步能到达的点,建反图然后拓扑

如果一个点标记为0代表必输点,为1代表必赢点,-1表示还没判断

对每一个入度为0的点,即没有可到达的下一个点的点进行判断


如果点u是必输点,说明无法进行下一步操作,所以,能到达这一点的点一定是必赢点,因为从必赢点跳到这一点,对手就无法进行下一步操作了。


如果点u是必赢点,那么u能到达的点一定至少有一个必输点。如果u的状态已经判断过,那就没有必要在判断,如果是-1 就设置为0

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<map>
#include<algorithm>
#define INF 0x3f3f3f3f3f3f3f3f
#define mod 1000000007
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define endl &#39;\n&#39;
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 10;
int a[maxn], degree[maxn], vis[maxn];
int q[maxn];
vector<int>v[maxn];
int main() {
  int n;cin >> n;
  for (int i = 1;i <= n;++i)cin >> a[i];
  for (int i = 1;i <= n;++i) {
    for (int j = i;j <= n;j += a[i]) {
      if (a[j] > a[i]) {
        v[j].push_back(i);
        degree[i]++;
      }
    }
    for (int j = i;j >= 1;j -= a[i]) {
      if (a[j] > a[i]) {
        v[j].push_back(i);
        degree[i]++;
      }
    }
  }
  memset(vis, -1, sizeof(vis));
  queue<int>que;
  for (int i = 1;i <= n;++i) {
    if (!degree[i])que.push(i), vis[i] = 0;
  }
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    for (int i = 0;i < v[now].size();++i) {
      int t = v[now][i];
      if (vis[now] == 0) {
        vis[t] = 1;
      }
      else if (vis[now] == 1) {
        if (vis[t] == -1)vis[t] = 0;
      }
      degree[t]--;
      if (!degree[t])que.push(t);
    }
  }
  for (int i = 1;i <= n;++i) {
    if (vis[i] == 1)printf("A");
    else printf("B");
  }
  return 0;
}


目录
相关文章
|
9月前
|
算法
uva 10891 game of sum
题目链接 详细请参考刘汝佳《算法竞赛入门经典训练指南》 p67
17 0
|
机器学习/深度学习 C++
C++ 中的 std::next_permutation 和 prev_permutation
它用于将范围 [first, last) 中的元素重新排列为下一个字典序更大的排列。一个排列是 N! 元素可以采用的可能排列(其中 N 是范围内的元素数)。不同的排列可以根据它们在字典上相互比较的方式进行排序。代码的复杂度为 O(n*n!),其中还包括打印所有排列。
78 0
|
算法 索引
LeetCode 45. Jump Game II
给定一个非负整数数组,初始位置在索引为0的位置,数组中的每个元素表示该位置的能够跳转的最大部署。目标是以最小跳跃次数到达最后一个位置(索引)。
61 0
LeetCode 45. Jump Game II
|
算法 索引
LeetCode 55. Jump Game
给定一个非负整数数组,您最初定位在数组的第一个索引处。 数组中的每个元素表示该位置的最大跳转长度。 确定您是否能够到达最后一个索引。
77 0
LeetCode 55. Jump Game
|
人工智能 算法 大数据
|
算法
[LeetCode]--55. Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Dete
1245 0
LeetCode - 45. Jump Game II
45. Jump Game II  Problem's Link  ---------------------------------------------------------------------------- Mean:  给定一个数组a,玩家的初始位置在idx=0,玩家需要到达的位置是idx=a.
925 0