HackerRank "Permutation game"
A typical game theory problem - Recursion + Memorized Search
http://justprogrammng.blogspot.com/2012/06/interviewstreet-challenges-permutation.html#.VlEMrvmrTIU
#include<iostream>
#include<set>
using namespace std; // Bit Ops Utils
int set(int a, int i)
{
return a | << i;
}
int unset(int a, int i)
{
return a & (~( << i));
}
int get(int a, int i)
{
return ((a&( << i)) >> i);
} //
int array[]; // for results record
int a[]; //
int play(int a[], int n, int r)
{
if (array[r] != -) return array[r]; // Check increase
int last = , i;
for (i = ; i < n; i++)
{
if (get(r, i))
{
if (a[i]>last) last = a[i];
else break;
}
}
if (i == n)
{
array[r] = ;
return ;
} // Recursively check each possibility
int rec[] = { , };
for (int i = ; i<n; i++)
{
if (get(r, i))
{
int ret = play(a, n, unset(r, i));
rec[ret] = ;
}
}
array[r] = rec[]; return array[r]; } int main()
{
int T;
cin >> T; while (T--)
{
for (int i = ; i < ; i++)
array[i] = -; int n; cin >> n;
for (int i = ; i < n; i++)
cin >> a[i]; int res = play(a, n, ( << n) - );
if (!res) cout << "Bob\n";
else cout << "Alice\n";
}
}
HackerRank "Permutation game"的更多相关文章
- Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Palindrome Permutation II 回文全排列之二
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
- [LeetCode] Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
- [LeetCode] Permutation Sequence 序列排序
The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- [LeetCode] Next Permutation 下一个排列
Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...
- Leetcode 60. Permutation Sequence
The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...
- UVA11525 Permutation[康托展开 树状数组求第k小值]
UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...
- Permutation test: p, CI, CI of P 置换检验相关统计量的计算
For research purpose, I've read a lot materials on permutation test issue. Here is a summary. Should ...
- 日常小测:颜色 && Hackerrank Unique_colors
题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...
随机推荐
- dbd到mongo的序列化问题及稳定性
最近工作压力有点大,情绪偶然会失控,要好好反省一下自己. 上周本来打算写CSP相关的东西,但是目前做得还不够多,积累的经验不足,就放弃了.中间找到很好的参考资料,一并放在这里.一篇是比较短的论文,可以 ...
- Core Java Volume I — 5.1. Classes, Superclasses, and Subclasses
5.1. Classes, Superclasses, and SubclassesLet's return to the Employee class that we discussed in th ...
- 使用Jmeter测试MySQL性能——(1)连接配置
在搭建MySQL集群之后需要测试集群的性能究竟如何,采用Apache的测试工具Jmeter进行测试,本文主要介绍主要实现Jmeter配置连接到MySQL. 安装相应的软件 首先Jmeter是基于Jav ...
- python学习笔记 - assert用法
[转自]http://blog.sina.com.cn/s/blog_76e94d210100vz37.html 1.assert语句用来声明某个条件是真的. 2.如果你非常确信某个你使用的列表中 ...
- 正则表达式中参数g、i、m的作用(share)
参数 g g 只影响于 exec.match 方法. 若不指定 g,则:每次调用 exec 都只返回第一个匹配:match 也是只返回第一个匹配. 若指定 g,则:每次调用 exec 都从上一个匹配之 ...
- [转载]新功能:用微软的Live Writer离线写博文
原文地址:Writer离线写博文">新功能:用微软的Live Writer离线写博文作者:新浪博客 Writer离线写博文" title="[转载]新功能:用微软的 ...
- UpdatePanel与$.function()同时使用问题
在.NET中使用了UpdatePanel,里面的输入框使用了jQuery的日历选择器,接下来介绍下两者同时使用的一些细节及问题的解决方法,感兴趣的各位可以参考下哈 今天,在.NET中使用了Update ...
- JS初学之-效果没出来怎么办?-alert函数测试
一般出了问题之后,有经验的高手一眼就可以看出来,但是对于我们初学者来说,利用alert函数不失为一个好方法. 这时我们要利用逐行测试的方法,在任意一句代码下加alert,如果可以弹出来就说明上面的没有 ...
- Hibernate的三种常用检索方式
Hibernate 提供了以下几种检索对象的方式 ¨ 导航对象图检索方式: 根据已经加载的对象导航到其他对象 ¨ OID 检索方式: 按照对象的 OID 来检索对象 ¨ ...
- scala言语基础学习三(面向对象编程)
定义一个简单的类 //定义类,包含field以及方法 自定义的getter 和setter 仅仅暴露field的getter和setter方法 private[this]的使用 (只能在当前实例中使用 ...