本文共 2106 字,大约阅读时间需要 7 分钟。
对于给定的数组 a1, a2, a3, ..., an,我们需要判断在特定规则下,谁会获胜。规则是:玩家从数组的两端开始交替取值,每次取值时必须满足当前值不为零。如果玩家无法取值,则输掉游戏。
这个问题可以通过分析数组的长度和元素的分布来解决。具体来说,我们需要考虑以下两种情况:
长度为偶数的情况:
长度为奇数的情况:
需要注意的是,由于这是一个环形结构,需要同时考虑从两端开始取值的双向压迫情况。
#includeusing namespace std;typedef long long ll;typedef long double lf;typedef unsigned long long ull;typedef pair P;const int inf = 0x7f7f7f7f;const ll INF = 1e16;const int N = 1e3 + 10;const ull base = 131;const int mod = 998244353;inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f;}inline string readstring() { string str; char s = getchar(); while (s == ' ' || s == '\n' || s == '\r') { s = getchar(); } while (s != ' ' && s != '\n' && s != '\r') { str += s; s = getchar(); } return str;}int random(int n) { return (int)(rand() * rand()) % n;}void writestring(string s) { int n = s.size(); for (int i = 0; i < n; i++) { printf("%c", s[i]); }}bool is_prime(int n) { if (n < 2) return false; for (int i = 2; i * i <= n; i++) { if (n % i == 0) return false; } return true;}int a[N];int main() { int n = read(); for (int i = 1; i <= n; i++) { a[i] = read(); } int ret = 0; for (int i = 1; i <= n; i++) { ret += 1; if (a[i] == 0) break; } if (ret % 2 == 0) { puts("YES"); return 0; } ret = 0; for (int i = n; i >= 1; i--) { ret += 1; if (a[i] == 0) break; } if (ret % 2 == 0) { puts("YES"); return 0; } puts("NO"); return 0;}
输入处理:
n 和数组元素 a。计算先手获胜次数:
计算后手获胜次数:
结果判断:
通过这种逻辑判断,我们可以快速确定游戏的胜者。
转载地址:http://ekqp.baihongyu.com/