[DP][博弈][后缀和]JZOJ 5778 没有硝烟的战争

Description

被污染的灰灰草原上有羊和狼。有N只动物围成一圈,每只动物是羊或狼。
该游戏从其中的一只动物开始,报出[1,K]区间的整数,若上一只动物报出的数是x,下一只动物可以报[x+1,x+K]区间的整数,游戏按顺时针方向进行。每只动物报的数字都不能超过M。若一只动物报了M这个数,它所在的种族就输了。问以第i只动物为游戏的开始,最后哪种动物会赢?
 
 

Input

第一行输入三个正整数N,M,K。
接下来一行N个正整数,分别表示N只动物的种类,以顺时针的方向给出。0代表羊,1代表狼。
 
 

Output

一行输出N个整数,表示若从第i只动物开始,赢的动物的种类。同上,0代表羊,1代表狼。
 
 

Sample Input

Input 1
2 9 2
0 1
Input 2
6 499 5
1 0 0 1 1 0
Input 3
10 100 10
0 0 0 1 1 1 1 0 1 1
 

Sample Output

Output 1
0 1
Output 2
0 1 1 1 1 0
Output 3
1 1 1 1 1 1 1 1 1 1
 
 

Data Constraint

对于60%的数据,1 ≤ N, M, K ≤ 500。
对于100%的数据,1 ≤ N, M, K ≤ 5000。

分析

这题我居然打了SG函数。。(其实也是类SG函数吧)

打完才想起来SG函数只适用于双方是交替进行操作的,GG,爆零

然后正解是DP

设f[i][j]为第i位选择j数字的局面:0表示先手必败,1表示先手必胜

显然初始值f[][m]=0

然后显然转移方程为f[i][j]=f[i+1][j+1~j+k]……

分类讨论:

如果i+1为同类,那么j+1~j+k之间有必胜局面时,f[i][j]也必胜

异类则相反

如何得知j+1~j+k有无必胜局面?

我们用一个后缀和维护就行

#include <iostream>
#include <cstdio>
#include <memory.h>
using namespace std;
const int N=5001;
bool f[N][N];
int b[N],s[N][N];
int n,m,k;

int main() {
    freopen("vode.in","r",stdin);
    freopen("vode.out","w",stdout);
    scanf("%d%d%d",&n,&m,&k);
    for (int i=0;i<n;i++) scanf("%d",&b[i]);
    for (int j=m-1;j>0;j--)
    for (int i=0;i<n;i++) {
        if (b[i]==b[(i+1)%n])
        f[i][j]=(s[(i+1)%n][j+1]-s[(i+1)%n][min(m,j+k)+1])?1:0;
        else
        f[i][j]=(s[(i+1)%n][j+1]-s[(i+1)%n][min(m,j+k)+1])?0:1;
        s[i][j]=s[i][j+1]+f[i][j];
    }
    for (int i=0;i<n;i++) {
        bool ans=0;
        for (int j=1;j<=k;j++)
        ans|=f[i][j];
        printf("%d ",!(b[i]^ans));
    }
    fclose(stdin);fclose(stdout);
} 
View Code
在日渐沉没的世界里,我发现了你。
原文地址:https://www.cnblogs.com/mastervan/p/9445430.html