[JZOJ5778]【NOIP提高A组模拟2018.8.8】没有硝烟的战争
博弈DP6666.
设f[i][j]为第i个人,选数j是否有必胜的可能,如果必胜,值为1,否则值为0。
然后如果i+1与i是同一种族,那么只要f[i+1][j+1...j+k]中有一个是1, 那么f[i][j]是1.
如果不是同一种族,那么如果f[i+1][j+1...j+k]中有一个不是1, 那么f[i][j]是1.
所以记忆化一下,结果TLE,76分。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; inline int read() { int res=0;char c=getchar();bool f=0; while(!isdigit(c)) {if(c=='-')f=1;c=getchar();} while(isdigit(c))res=(res<<3)+(res<<1)+(c^48),c=getchar(); return f?-res:res; } int n, m, k; int a[5005*2]; int f[5005][5005]; int dp(int x, int y) { if (y == m) return f[x][y] = 0; if (f[x][y] != -1) return f[x][y]; int j = x + 1; if (j == n + 1) j = 1; int res = 0; for (register int i = y + 1 ; i <= min(y + k, m) ; i ++) if(dp(j, i) == (a[x] == a[j])) return f[x][y] = 1; return f[x][y] = 0; } int main() { freopen("vode.in", "r", stdin); freopen("vode.out", "w", stdout); n = read(), m = read(), k = read(); for (int i = 1 ; i <= n ; i ++) a[i] = read(); memset(f, -1, sizeof f); for (register int i = 1 ; i <= n ; i ++) printf("%d ",dp(i,1)?a[i]:!a[i]); return 0; }
可以用后缀和优化一下, 记录后缀和,如果i与i+1同族如果j+1到j+k的后缀和大于1,那么f[i][j]=1;
如果不同种族,那么j+1到j+k的后缀和不等于j+k-j,那么f[i][j] = 1;
其余情况f[i][j]=0;
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; inline int read() { int res=0;char c=getchar();bool f=0; while(!isdigit(c)) {if(c=='-')f=1;c=getchar();} while(isdigit(c))res=(res<<3)+(res<<1)+(c^48),c=getchar(); return f?-res:res; } int n, m, k; int a[5005]; int f[5005<<1][5005]; int hzh[5005<<1][5005]; int main() { freopen("vode.in", "r", stdin); freopen("vode.out", "w", stdout); n = read(), m = read(), k = read(); for (register int i = 1 ; i <= n ; i ++) a[i] = read(); for(int i = 1 ; i <= n + m ; i ++) a[i] = a[(i - 1) % n + 1]; for (register int i = m - 1; i >= 1 ; i --) { for(int j = n + m - 1; j >= 0 ; j --) { int t = j + 1; if (a[j] == a[t]) if ((hzh[t][i+1] - hzh[t][min(m, i + k) + 1] > 0)) f[j][i] = 1; if (a[j] != a[t]) if ((hzh[t][i+1] - hzh[t][min(m, i + k) + 1] != min(m, i + k) - i)) f[j][i] = 1; hzh[j][i] = hzh[j][i+1] + f[j][i]; } } for (register int i = 1 ; i <= n ; i ++) printf("%d ",f[i][1] ? a[i] : !a[i]); return 0; }