北邮OJ 1021. 16校赛-Stone Game
時間限制?4000 ms?內存限制?65536 KB
題目描述
? ? Alice and Bob are old friends in game theory. This afternoon they meet in Starbucks, each ordered a cup of coffee and decided to start a new game. They line?N?cups on the table, each containing?Ai?stones. Different from the usual stone games, each cup is numbered from?0?to?N?1, and has an integer?Ci?on it. The players in turn chooses one cup numbered?X?with stones and moves one stone to the cup numbered?Y?(Y∈{X?CX,X?CX+1,...,X?1}). Alice will be the first to move. The player who cannot move will lose the game. It is guaranteed that?1≤Ci≤i?for any?i≥1, and?C0=0.
輸入格式
? ? The input starts with an integer?T?(1≤T≤20)? ? ? ?, indicating the number of test cases.
?? ?For each test case, the first line contains an integer?N?(1≤N≤100000)? ? ? ?indicating the number of cups. The second line contains?N?integers, the?i-th integer indicates?Ai?(0≤Ai≤109)? ? ? ? ,?the number of stones in cups. The third line contains?N?integers, the?i-th integer indicates?Ci,?the integers?written on the cups.
輸出格式
? ? For each test case, if Alice will win however Bob moves, print a line 'Win' without quotes. If not, print a line 'Lose' without quotes.
輸入樣例
2 3 0 0 1 0 1 1 7 2 1 0 0 0 1 0 0 1 2 1 2 4 3輸出樣例
Lose Win 博弈題,典型求SG值異或出解,但本題n數值范圍10^5,且每個杯子的前序狀態包含多個,不能使用一般的O(N^2)的getSG來做。用線段樹的維護區間最小值功能來更新SG值的所有者,SG值最大10^5,線段樹開4倍空間,維護(更新)某一個SG值的當前所有者(也就是用線段樹求SG值),然后我們會發現因為每次只能移動1個石子,而且每次都至少可以從當前杯子移動到前一個杯子(杯子序號:偶數到奇數,奇數到偶數),那么杯子里的石子數如果是偶數,我們就可以不用考慮,不論你怎么從杯子里移動出去,我們都可以照葫蘆畫瓢跟著你移動,對結果不影響。最后把杯子里石子數是奇數的SG值異或起來即可。
#include<cstdio> #include<cmath> #include<algorithm> #define N 100050 using namespace std; int a[N]; int c[N]; int dat[N*4]; int sg[N];int init(int rot,int l,int r){dat[rot]=-1;if(l==r)return 1;int mid=(l+r)/2;init(rot*2,l,mid);init(rot*2+1,mid+1,r); } int inser(int rot,int pos,int x,int l,int r){if(l==r){dat[rot]=x;return 1;}int mid=(l+r)/2;if(pos<=mid)inser(rot*2,pos,x,l,mid);else inser(rot*2+1,pos,x,mid+1,r);dat[rot]=min(dat[rot*2],dat[rot*2+1]); } int q_min(int rot,int x,int l,int r){if(l==r){return l;}int mid=(l+r)/2;if(dat[rot*2]<x){return q_min(rot*2,x,l,mid);}else{return q_min(rot*2+1,x,mid+1,r);} }int main(){int t,n,i;for(scanf("%d",&t);t--;){scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);for(i=0;i<n;i++)scanf("%d",&c[i]);init(1,0,n);int sg_num=0;int i,j;for(i=0;i<n;i++){sg[i]=q_min(1,i-c[i],0,n);inser(1,sg[i],i,0,n);if(a[i]%2==1){sg_num=sg_num^sg[i];}}if(sg_num)printf("Win\n");else printf("Lose\n");}return 0; }
總結
以上是生活随笔為你收集整理的北邮OJ 1021. 16校赛-Stone Game的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 北邮OJ 1010. 16校赛-Bina
- 下一篇: 北邮OJ 1022. 16校赛-Sabe