1、一维 题目
#include <cstdio>
#include <cstring>
const int SZ = 5007;
int dir[SZ];
bool f[SZ];
int calc(int k, int n) {
memset(f, false, sizeof f);
int ans = 0, sum = 0;
for (int i = 1; i+k-1 <= n; ++i) {
if (dir[i]+sum & 1)
++ans, f[i] = true;
sum += f[i];
if (i-k+1 >= 1)
sum -= f[i-k+1];
}
for (int i = n-k+2; i <= n; ++i) {
if (dir[i]+sum & 1) return -1;
if (i-k+1 >= 1) sum -= f[i-k+1];
}
return ans;
}
int main() {
int n;
scanf("%d", &n);
char x[2];
for (int i = 1; i <= n; ++i)
scanf("%s", x), dir[i] = x[0]=='B' ? 1 : 0;
int M = n, K;
for (int i = 1; i <= n; ++i) {
int cnt = calc(i, n);
if (cnt>0 && M>cnt)
M = cnt, K = i;
}
printf("%d %d\n", K, M);
return 0;
}
2、二维 题目
#include <cstdio>
#include <cstring>
int dirx[5] = {-1, 1, 0, 0, 0}, diry[5] = {0, 0, -1, 0, 1};
int g[17][17], ans[17][17], flip[17][17];
int M, N;
int get(int x, int y) {
int c = g[x][y];
for (int i = 0; i < 5; ++i) {
int xx = x + dirx[i], yy = y + diry[i];
if (xx>=1 && xx<=M && yy>=1 && yy<=N)
c += flip[xx][yy];
}
return c & 1;
}
int calc() {
for (int i = 2; i <= M; ++i)
for (int j = 1; j <= N; ++j)
if (get(i-1, j))
flip[i][j] = 1;
for (int j = 1; j <= N; ++j)
if (get(M, j)) return -1;
int res = 0;
for (int i = 1; i <= M; ++i)
for (int j = 1; j <= N; ++j)
res += flip[i][j];
return res;
}
int main() {
scanf("%d%d", &M, &N);
for (int i = 1; i <= M; ++i)
for (int j = 1; j <= N; ++j)
scanf("%d", &g[i][j]);
int res = 500;
for (int i = 0; i < 1<<N; ++i) {
memset(flip, 0, sizeof flip);
for (int j = 0; j < N; ++j)
flip[1][N-j] = i>>j & 1;
int num = calc();
if (num>=0 && res>num) {
res = num;
memcpy(ans, flip, sizeof flip);
}
}
if (res == 500) puts("IMPOSSIBLE");
else for (int i = 1; i <= M; ++i)
for (int j = 1; j <= N; ++j)
printf("%d%c", ans[i][j], j==N?'\n':' ');
return 0;
}
老师上课讲的题,目前还没完全掌握,等有时间在仔细想想,,哎,时间真的不够用,主要还是脑子笨= =