A. Lesha and array splitting(Codeforces 754A)
思路
首先当且仅当数组里所有的元素都是0时数组无法被划分为非零子数组。剩下的情况都是可以的,只要构造出一种合法情况就行了。最简单的是不对数组做划分,这种“划分”方法是合法的当且仅当数组的所有元素的和不为0。那么,当数组中所有元素的和为0时只要将数组划分成两段,保证第一段元素之和不为0即可。
代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 110;
int flag, n, pos, a[maxn];
int main() {
// freopen("data.txt", "r", stdin);
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
flag |= a[i];
// 处理前缀和
a[i] += a[i-1];
// 记录前缀和非0的位置
if(a[i] != 0) {
pos = i;
}
}
// 当所有元素都为0时
if(flag == 0) {
puts("NO");
return 0;
}
puts("YES");
// 当所有元素的和为0时
if(a[n] != 0) {
printf("1\n1 %d\n", n);
return 0;
}
printf("2\n1 %d\n%d %d\n", pos, pos + 1, n);
return 0;
}
B. Ilya and tic-tac-toe game(Codeforces 754B)
思路
枚举矩阵内所有的“三连块”,当其中至少有两个x且没有o的时候就输出YES否则输出NO。
代码
#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, -1, 0, 1};
const int dy[] = {
0, 1, 1, 1};
char ch, G[10][10];
bool ok() {
for(int i = 1; i <= 4; i++) {
for(int j = 1; j <= 4; j++) {
// 在横,纵,主副对角线上找三连子
for(int k = 0; k < 4; k++) {
int x1 = i + dx[k];
int y1 = j + dy[k];
int x2 = x1 + dx[k];
int y2 = y1 + dy[k];
// 若存在两个以上的x且不存在o
if(G[i][j] + G[x1][y1] + G[x2][y2] >=