Description
两个长度为2n的01串,两个人轮流开始取数字,每次只能取两个人之前都没取过的位置的数字,n次后两个人就得到了两个长度为n的数字,谁的数字大谁赢,两个人足够机智,问是先手必胜还是后手必胜还是平局
Input
第一行一整数n,之后两个长度为2n的01串(1<=n<=1e6)
Output
如果先手必胜则输出First,如果后手必胜则输出Second,如果平局则输出Draw
Sample Input
2
0111
0001
Sample Output
First
Solution
谁拿的1多谁就赢,所以去分析最优情况下两个人可以拿到的最多的1的数量,令
cnt1:第一个串是1第二个串是0的位置数
cnt2:第一个串是0第二个串是1的位置数
cnt3:第一个串是1第二个串是1的位置数
最优策略为两个人先去拿“公共”的1,然后再去拿“自己”的1,在拿公共的1时先手会比后手多拿cnt3%2个1,那么cnt1=cnt1+cnt3%2+cnt3/2即为先手可以拿的1的数量,cnt2=cnt2+cnt3/2为后手可以拿的1的数量,注意到先手有一个优势就是如果后手恰比先手多拿一个1的话,那么先手可以先把这个1位置拿了使得后手取不到这个1把局面变成平局,所以如果cnt2-1=cnt1也是平局,只有当cnt2-cnt1 > 1时后手必胜,cnt2-cnt1 < 0时先手必胜,cnt2=cnt1或cnt2=cnt1+1时是平局
Code
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
#define INF 0x3f3f3f3f
#define maxn 2111111
int n;
char a[maxn],b[maxn];
int main()
{
while(~scanf("%d",&n))
{
scanf("%s",a);
scanf("%s",b);
int cnt1=0,cnt2=0,cnt3=0;
for(int i=0;i<2*n;i++)
{
if(a[i]=='1'&&b[i]=='1')cnt3++;
else if(a[i]=='1')cnt1++;
else if(b[i]=='1')cnt2++;
}
cnt1+=cnt3%2;
if(cnt2-1==cnt1)cnt2--;
if(cnt1>cnt2)printf("First\n");
else if(cnt1==cnt2)printf("Draw\n");
else printf("Second\n");
}
return 0;
}