【解题报告】Codeforces Round #390 (Div. 2)

题目链接


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] >= 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值