Codeforces 1162E Thanos Nim(博弈)

一道有意思的博弈题。首先我们考虑一种必败情况,那就是有一方拿光了一堆石子,显然对方是必胜,此时对方可以全部拿走其中的n/2,那么轮到自己时就没有n/2堆,所以此时是必败态。我们先对所有石子堆sort,设最少的石子堆a[i]的石子数为a,有b堆这样的石子,当b<=n/2的时候,先手可以将另外一半的石子拿走至与前一半石子堆的数量一致( {a1 a2 ... an/2 a/n2+1... an} 变成 {a1 a2 ...an/2 a1 a2.... an/2} ) ,那么接下来无论对方拿走多少石子,我们都可以模仿对方的策略,显然,对方会先拿控一堆石子走到必败态,那么先手就必赢。在考虑一种情况,当b>n/2时,也就是最少的石子堆数量占一半以上,那么无论先手怎么取,后手都可以将剩下的石子堆保持最少的石子堆有一半以上(无论对方怎么取,我们只需要把n/2个石子堆数量保持与最小的一致),那么此时先手是一定会先拿空一堆石子,那么先手必败。综上,也就是判断最少石子堆数量是否有一半以上即可。

 1 //      ——By DD_BOND
 2 
 3 //#include<bits/stdc++.h>
 4 #include<functional>
 5 #include<algorithm>
 6 #include<iostream>
 7 #include<sstream>
 8 #include<iomanip>
 9 #include<climits>
10 #include<cstring>
11 #include<cstdlib>
12 #include<cstddef>
13 #include<cstdio>
14 #include<memory>
15 #include<vector>
16 #include<cctype>
17 #include<string>
18 #include<cmath>
19 #include<queue>
20 #include<deque>
21 #include<ctime>
22 #include<stack>
23 #include<map>
24 #include<set>
25 
26 #define fi first
27 #define se second
28 #define MP make_pair
29 #define pb push_back
30 #define INF 0x3f3f3f3f
31 #define pi 3.1415926535898
32 #define lowbit(a)  (a&(-a))
33 #define lson l,(l+r)/2,rt<<1
34 #define rson (l+r)/2+1,r,rt<<1|1
35 #define Min(a,b,c)  min(a,min(b,c))
36 #define Max(a,b,c)  max(a,max(b,c))
37 #define debug(x)  cerr<<#x<<"="<<x<<"
";
38 
39 using namespace std;
40 
41 typedef long long ll;
42 typedef pair<int,int> P;
43 typedef pair<ll,ll> Pll;
44 typedef unsigned long long ull;
45 
46 const ll LLMAX=2e18;
47 const int MOD=1e9+7;
48 const double eps=1e-8;
49 const int MAXN=1e6+10;
50 
51 inline ll sqr(ll x){ return x*x; }
52 inline int sqr(int x){ return x*x; }
53 inline double sqr(double x){ return x*x; }
54 ll __gcd(ll a,ll b){ return b==0? a: __gcd(b,a%b); }
55 ll qpow(ll a,ll n){ll sum=1;while(n){if(n&1)sum=sum*a%MOD;a=a*a%MOD;n>>=1;}return sum;}
56 inline int dcmp(double x){    if(fabs(x)<eps) return 0;    return (x>0? 1: -1); }
57 
58 int a[MAXN];
59 
60 int main(void)
61 {
62     ios::sync_with_stdio(false);    cin.tie(0);   cout.tie(0);
63     int n,cnt=0;  cin>>n;
64     for(int i=0;i<n;i++)    cin>>a[i];
65     sort(a,a+n);
66     if(a[0]==a[n/2])    cout<<"Bob"<<endl;
67     else    cout<<"Alice"<<endl;
68     return 0;
69 }
原文地址:https://www.cnblogs.com/dd-bond/p/10821654.html