您的位置:首页 > 其它

hackerrank_Permutation game

2014-11-27 20:49 344 查看
https://www.hackerrank.com/challenges/permutation-game

面包大大给的题目,想了两个多小时还是人家给我的想法T_T,太弱了,想到了LIS,图论之类的,但是都没能实现,最后还是用的暴力,求解:如果n>32之后,这个题怎么做?

如果牛牛们有想法,请评论给我,谢谢!

#include<iostream>
#include<cstring>
using namespace std;
bool flag[3000000];
int main()
{
int n;
int t;
cin>>t;
int a[15];
while(t--)
{
memset(flag,0,sizeof(flag));
cin>>n;
for (int i=0; i<n; i++)
cin>>a[i];
for (int i=0; i<=n; i++)
flag[1<<i]=true;
for (int i=1; i<(1<<n); i++)
{
if (flag[i]) continue;
int temp1=-1;
bool flag_inc=true;
for (int k=0; (1<<k)<=i; k++)
{
if (!(i&(1<<k))) continue;
if (a[k]<=temp1)
{
flag_inc=false;
break;
}
else
temp1=a[k];
}
if (flag_inc)
{
flag[i]=true;
continue;
}
bool flag_worl=false;
for (int j=0; (1<<j)<=i; j++)
{
int temp=i;
if (temp&(1<<j)) temp-=(1<<j);
if (flag[temp])
{
flag_worl=true;
break;
}
}
if (flag_worl) flag[i]=false;
else
flag[i]=true;
}
if (flag[(1<<n)-1])
cout<<"Bob"<<endl;
else
cout<<"Alice"<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hackerrank 博弈