题目
交互题,初始给出n(n<=2e3),l,r(1<=l<=r<=n)
表示初始时桌面上有n张牌,1到n依次排列,选手和交互机可以轮流取,
每次可以选择一个初始的位置x,并在[l,r]内选择一个数y,
表示本次要将x,x+1,...,x+y-1这连续的y张牌都取走,
如果有一方不能操作了,则另一方胜利,现在需要你战胜交互机,模拟交互过程
初始时,你可以输出First或Second,表示你先手或后手
到你出牌时,你输出(x,y),表示本次要将x,x+1,...,x+y-1这连续的y张牌都取走
并读入(x,y),表示交互机要将x,x+1,...,x+y-1这连续的y张牌都取走
当你读入的是(0,0)时,代表你获胜,此时终止读入
思路来源
灵茶群
题解
一眼multi-sg,即当前游戏的操作会使得局面分裂成多个子游戏
hdu3980 Paint Chain(博弈/Multi-SG)_Code92007的博客-CSDN博客
但是n<=2e3,multi-sg的复杂度只能解决单点,比如l=r的情形,
此时,先打出sg表,然后只需暴力set维护当前有哪些线段,
当该你操作时,枚举所有线段,枚举所有切割可能,暴力找到一个sg=0的后继即可
由于单次操作时,所有线段的所有切割可能是不超过2e3的,所以复杂度可行
而当l<r时,注意到,第一步一定可以选r或r-1,二者必有一个和n同奇偶
总可以选择First,第一步通过将初始局面分割为两个对称局面,后续模仿交互机即可
思路想到之后就不难了,implementation会是一个难点,交互题也很难debug
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84#include<bits/stdc++.h> using namespace std; const int N=2e3+10; typedef pair<int,int> P; int n,l,r,a,b,sg[N],f; bool vis[N]; set<P>now; void ask(int x,int y){ cout<<x<<" "<<y-x+1<<endl; } void getsg(int m){ for(int i=0;i<m;++i)sg[i]=0; sg[m]=1; for(int i=m+1;i<=n;++i){ memset(vis,0,sizeof vis); for(int j=0;j<=i-m;++j) vis[sg[j]^sg[i-m-j]]=1; for(int j=0;;++j) if(!vis[j]){ sg[i]=j; break; } } } void op1(){ for(auto &v:now){ int x=v.first,y=v.second,z=y-x+1; f^=sg[z]; for(int i=0;i<=z-r;++i){ int L=i,R=z-r-i; if(!(f^sg[L]^sg[R])){ now.erase(P(x,y)); if(L>=r)now.insert(P(x,x+L-1)); if(R>=r)now.insert(P(y-R+1,y)); ask(x+L,y-R); f=0; return; } } f^=sg[z]; } } void op2(int a,int b){ for(auto &v:now){ int x=v.first,y=v.second,z=y-x+1; if(x<=a && b<=y){ f^=sg[z]; now.erase(P(x,y)); if(a-x>=r)f^=sg[a-x],now.insert(P(x,a-1)); if(y-b>=r)f^=sg[y-b],now.insert(P(b+1,y)); return; } } } int main(){ cin>>n>>l>>r; if(l<r || (l==r && (n-r)%2==0)){ int z=(n-r)%2==0?r:r-1; cout<<"First"<<endl; ask((n-z)/2+1,(n-z)/2+z); while(cin>>a>>b){ if(!a && !b)return 0; b=a+b-1; ask(n+1-b,n+1-a); } } else{ getsg(r); now.insert(P(1,n)); f=sg[n]; if(!sg[n])cout<<"Second"<<endl; else{ cout<<"First"<<endl; op1(); } while(cin>>a>>b){ if(!a && !b)return 0; b=a+b-1; op2(a,b); op1(); } } return 0; }
最后
以上就是风趣酒窝最近收集整理的关于AtCoder Beginner Contest 278 G.Generalized Subtraction Game(思维题/博弈 multi-sg)的全部内容,更多相关AtCoder内容请搜索靠谱客的其他文章。
发表评论 取消回复