option=com_onlinejudge&Itemid=8&page=show_problem&problem=4336" style="">题目链接:uva 1561 - Cycle Game

题目大意:给出一个环,每次从起点開始,能够选择一个权值非0的边移动,移动后减掉权值至少1点。

不能移动的为失败。

解题思路:

  • 1:有0的情况,假设有方向离权值为0的边的步数为奇数,则为必胜。否则必败。
  • 2:无0的情况,奇数边必胜;
  • 3:有1的情况。同0的推断一样;
  • 4:无1的情况,仅仅剩偶数边的情况,必败;
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int maxn = 30; int N, arr[maxn]; bool check (int k) {
int p = -1;
while (arr[++p] != k);
int q = N;
while (arr[--q] != k);
q = N - 1 - q;
return (p&1) || (q&1);
} bool judge () {
for (int i = 0; i < N; i++) {
if (arr[i] == 0)
return check(0);
} if (N&1)
return true; for (int i = 0; i < N; i++) {
if (arr[i] == 1)
return check(1);
}
return false;
} int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%d", &N);
for (int i = 0; i < N; i++)
scanf("%d", &arr[i]);
printf("%s\n", judge() ? "YES" : "NO");
}
return 0;
}

uva 1561 - Cycle Game(推理)的更多相关文章

  1. UVA 11090 - Going in Cycle!!(Bellman-Ford)

    UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...

  2. UVA - 11090 - Going in Cycle!!(二分+差分约束系统)

    Problem  UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...

  3. UVA 11090 Going in Cycle!! SPFA判断负环+二分

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  4. UVA 11246 - K-Multiple Free set(数论推理)

    UVA 11246 - K-Multiple Free set 题目链接 题意:一个{1..n}的集合.求一个子集合.使得元素个数最多,而且不存在有两个元素x1 * k = x2,求出最多的元素个数是 ...

  5. UVA 11090 - Going in Cycle!! SPFA

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  6. UVa 11090 Going in Cycle!!【Bellman_Ford】

    题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...

  7. UVA 11090 Going in Cycle!!(二分答案+判负环)

    在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...

  8. UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)

    题意: 给定一个n个点m条边的带权有向图,求平均权值最小的回路的平均权值? 思路: 首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少.一般这种就是二分猜平均权值了,因为环在哪也难以找出来 ...

  9. UVA 11090 Going in Cycle!!

    要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了. 按照w1+w2+w3+….wn < n*ave的不等式,也就是(w1-ave) + (w2-ave) +…..(wn-ave) & ...

随机推荐

  1. leetcode Contains Duplicate python

    Given an array of integers, find if the array contains any duplicates. Your function should return t ...

  2. delphi R3下 跨进程获取DLL信息 NtQueryInformationProcess

    unit APIUnit; { GetProcessModuleHandle API Unit Ring3调用NtQueryInformationProcess实现跨进程获取DLL句柄 } inter ...

  3. php-Eclipse对php中的namespace关键字报语法错误的问题

    namespace是php5.3以上才支持的, 解决办法:升级IDE 切换Eclipse中的php版本 Window  > Preferences  > PHP  > PHP Int ...

  4. IOS 物理引擎

    来自IOS7 by tutorials   下面是个人的一点翻译总结 1,首先在初始化方法李画一个方块 UIView* square = [[UIView alloc] initWithFrame: ...

  5. asp.net 多站点共享StateServer Session

    <sessionState mode="StateServer" stateConnectionString="tcpip=127.0.0.1:42424" ...

  6. 编程器NAND Flash 技术入门

    NAND Flash分类 SLC(Single-Level Cell)架构:单一储存单元(Cell)可储存1bit data MLC(Multi-Level Cell)架构:单一储存单元(Cell)可 ...

  7. CentOS ips bonding

    centos ip bonding 一个网卡多个ips,多个网口一个ip 1,配置一个网卡多ips的情况cp /etc/sysconfig/network-scripts/ifcfg-eth0 /et ...

  8. openStack icehouse for centos6.4 production Env 实战

    production Env brief Overview: Management Node:  controller.cc 10.114.100.115 Neutron Network Node:  ...

  9. MyMVC框架的使用

    1)在web.config 中system.web 节点下加入例如以下代码 <pages controlRenderingCompatibilityVersion="4.0" ...

  10. ZOJ 3822 Domination

    题意: 一个棋盘假设每行每列都有棋子那么这个棋盘达到目标状态  如今随机放棋子  问达到目标状态的期望步数 思路: 用概率来做  计算第k步达到目标状态的概率  进而求期望  概率计算方法就是dp  ...