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大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...
随机推荐
- 【avalon】createMap
/** * Creates a new object without a prototype. This object is useful for lookup without having to * ...
- bold, big, blink
- 华为"128为大整数相加"机试题
最近正直春招,偶尔接触到了华为的这道大整数相加的测试题,在网上找了一个算法,然后自己尝试进行了优化,最后也对memmove()函数效率有了进一步把握. #include <time.h># ...
- OpenvSwitch架构
Openvswitch的架构 数据库结构和OVS-VSCTL # ps aux | grep openvswitch root 1117 0.0 0.0 21200 1580 ? ...
- ZOJ 3805--解题报告
题目相关: 3805相关链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 在二维的矩形上, 机器通过管道(pipe ...
- hdu 1548 (dijkstra解法)(一次AC就是爽)
恭喜福州大学杨楠获得[BestCoder Round #4]冠军(iPad Mini一部) <BestCoder用户手册>下载 A strange lift Time Limit: 200 ...
- POJ1149 PIGS (网络流)
PIGS Time Limit: 1000MS M ...
- JAVA基本语义简介
1.标识符 标识符可以有字母.数字.下划线(_).美元符($)组成,但不能包含@.%.空格等其他特殊符,不能以数字开头. 标识符不能是JAVA关键字和保留字(JAVA预留的关键字,以后的升级版中有可能 ...
- FZU-2216 The Longest Straight (二分枚举)
题目大意:给n个0~m之间的数,如果是0,那么0可以变为任意的一个1~m之间的一个数.从中选出若干个数,使构成一个连续的序列.问能构成的最长序列的长度为多少? 题目分析:枚举连续序列的起点,二分枚举二 ...
- 布局转换:文档流->绝对定位
布局转换:文档流->绝对定位(详见妙味JS高级教程,运动课程第6课20分钟起)比如一个DIV中有三张图片并排,个数不确定的布局.需要鼠标移上去图片从中心放大,只使用float:left布局在放大 ...