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"的更多相关文章

  1. Permutation Sequence

    The set [1,2,3,-,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  2. [LeetCode] Palindrome Permutation II 回文全排列之二

    Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...

  3. [LeetCode] Palindrome Permutation 回文全排列

    Given a string, determine if a permutation of the string could form a palindrome. For example," ...

  4. [LeetCode] Permutation Sequence 序列排序

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  5. [LeetCode] Next Permutation 下一个排列

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  6. 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 ...

  7. UVA11525 Permutation[康托展开 树状数组求第k小值]

    UVA - 11525 Permutation 题意:输出1~n的所有排列,字典序大小第∑k1Si∗(K−i)!个 学了好多知识 1.康托展开 X=a[n]*(n-1)!+a[n-1]*(n-2)!+ ...

  8. 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 ...

  9. 日常小测:颜色 && Hackerrank Unique_colors

    题目传送门:https://www.hackerrank.com/challenges/unique-colors 感谢hzq大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...

随机推荐

  1. 简明python教程 --C++程序员的视角(九):函数式编程、特殊类方法、测试及其他

    函数式编程 Lambda exec,eval和assert语句,repr函数   lambda语句 用来创建简短的单行匿名函数 print_assign = lambda name, value: n ...

  2. angularjs 不同的controller之间值的传递

    Sharing data between controllers in AngularJS I wrote this article to show how it can possible to pa ...

  3. 多层CCLayer的touch冲突解决

    一般通过layer. setTouchPriority()方法来设置 touch优先级,数值越小,优先级越高,但有时多人开发过程中,多层layer叠在一起,无法通过setTouchPrority()来 ...

  4. 从Wordpress迁移到Jekyll

    http://pinkyjie.com/2013/10/24/migrate-from-wordpress-to-jekyll/ 上周末闲着没事干突然想把博客从Wordpress迁移到Github p ...

  5. CentOS下解决”用户账户is not in the sudoers file“问题

    如上图,在当前用户cent(我的用户名)下使用sudo命令时,提示"cent is not in the sudoers file. This incident will be report ...

  6. Matlab 矩阵卷积理解(转载)

    转载自:http://blog.csdn.net/andrewseu/article/details/51783181 在图像处理的过程中,经常会看到矩阵卷积的概念,比如说用一个模板去和一张图片进行卷 ...

  7. 光流算法:Brox算法(转载)

    参考论文:1. High Accuracy Optical Flow Estimation Based on a Theory for Warping, Thomas Box, ECCV20042. ...

  8. Spring MVC数组绑定

    需求:商品批量删除,用户在页面选择多个商品,批量删除. 关键:将页面选择(多选)的商品id,传到controller方法的形参,方法形参使用数组接收页面请求的多个商品id // 批量删除 商品信息 @ ...

  9. php 倒计时程序

    <html>  <head>  <meta http-equiv="Content-Type" content="text/html; ch ...

  10. Android——进度对话框

    java类代码: //普通进度对话框 public void bt8_onClick(View v) { final ProgressDialog progressDialog = new Progr ...