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大神找来的这道题. 考虑点分治(毕竟是路经统计),对于每一个颜色,它的贡献 ...
随机推荐
- 67. Add Binary
public class Solution { public String addBinary(String a, String b) { char[] aa=a.toCharArray(); cha ...
- java的getClass()函数
Java反射学习 所谓反射,可以理解为在运行时期获取对象类型信息的操作.传统的编程方法要求程序员在编译阶段决定使用的类型,但是在反射的帮助下,编程人员可以动态获取这些信息,从而编写更加具有可移植性的代 ...
- Spring AOP 实现功能权限校验功能
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 使用拦截器实现未登录时跳转到登录界面的功能 1 拦截器SecurityInterceptor 2spring-mvcxml拦 ...
- HTTP详解(1)-工作原理
出处 http://blog.csdn.net/hguisu/article/details/8680808#t0 1. HTTP简介 HTTP协议(HyperText Transfer Protoc ...
- Codeforces Round #127 (Div. 2)
A. LLPS 长度最大10,暴力枚举即可. B. Brand New Easy Problem 枚举\(n\)的全排列,按题意求最小的\(x\),即逆序对个数. C. Clear Symmetry ...
- Linux 下WordPress FTP帐号解决办法
自己用Ubuntu搭建WordPress后在更换主题时提示需要输入FTP帐号和密码,解决办法主要是把WordPress主目录的权限所有者弄为Apache: 找到apache服务所使用的用户名和用户组 ...
- tar学习使用心得
tar如何解压文件到指定的目录?#tar zxvf mysql.tar.gz -C /home/aaa 暂时用到这,所以学到这,日后再更新 近日用tar解压一个文件时发现这样的错误: # tar -z ...
- CLR thread pool
Thread Pooling https://msdn.microsoft.com/en-us/library/windows/desktop/ms686756(v=vs.85).aspx Threa ...
- GPU的革命
CUDA 线程执行模型分析(一)招兵------ GPU的革命 CUDA 线程执行模型分析(二)大军未动粮草先行------GPU的革命 CUDA硬件实现分析(一)------安营扎寨-----GPU ...
- PHP-关于$_SERVER
类似于Nginx中的请求头,所有header,都可以使用 $http_xxx来使用,比如$http_accept,甚至包括自定义的,比如,$http_x_forwarded_host proxy_se ...