Codeforces Round #766 (Div. 2) (A-C) 题解

更好的阅读体验:http://www.abmcar.top/archives/codeforcesround766div2a-cti-jie

完整代码:https://github.com/abmcar/ACM/tree/master/OpenjudgeNow/Codeforces/Codeforces%20Round%20%23766%20(Div.%202)

A. Not Shading

在这里插入图片描述
题目大意:
给你一个n*m的板子,每个格子分为黑色和白色,你可以一次选择多个黑色格子让他们的一整行或者一整列变成黑色,问最少多少次可以让(X,Y)的位置变黑
思路:
注意题目说一次可以选择多个黑色格子进行变黑,因此分为这几种情况
1.目标位置已经是黑色 0操作
2.目标位置同行或者同列有黑 1次操作
3.目标位置同行同列都没有黑 但板子上有黑, 此时我们先随便变一行黑,然后再变列黑 2次操作
4.无黑 操作不来
代码:

void work()
{
    cin >> n >> m;
    int t1, t2;
    cin >> t1 >> t2;
    for (int i = 1; i <= n; i++)
        cin >> bd[i]+1;
    bool ok = true;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= m; j++)
            if (bd[i][j] == 'B')
                ok = false;
    if (ok)
    {
        cout << -1 << endl;
        return;
    }
    if (bd[t1][t2] == 'B')
    {
        cout << 0 << endl;
        return;
    }
    ok = true;
    for (int i = 1; i <= n; i++)
        if (bd[i][t2] == 'B')
            ok = false;
    for (int j = 1; j <= m; j++)
        if (bd[t1][j] == 'B')
            ok = false;
    if (!ok)
        cout << 1 << endl;
    else
        cout << 2 << endl;
}

B. Not Sitting

在这里插入图片描述
题目大意:
有一个nm的板子,有两个人A和B,A可以禁止B坐其中k个位置,B想和A坐的最近,A想和B坐的最远,A先选择禁止的位置,然后B再选坐,A再选一个离B最远的位置,问当k分别为0,1,2,…,nm-1时他们的最远距离是多少
(舔狗不得house)
思路:
(感觉我这个有些麻烦,不一定是最优解)
已知B坐中间是最优的,因此A禁止也是先禁止中间,用bfs的思想从中间bfs到边缘,离中间越远,答案越大
因为n,m都是1e5大小的,所有不能直接开数组,这里我用来了vector动态开
代码:

int nx[] = {1, 0, -1, 0};
int ny[] = {0, 1, 0, -1};
vector<int> M[Maxn];

void work()
{
    cin >> n >> m;
    queue<pair<int, int>> Q;
    for (int i = 1; i <= n; i++)
        M[i].resize(m + 1), M[i].clear();
    map<int, int> ans;
    for (int i = (n + 1) / 2; i <= (n + 2) / 2; i++)
        for (int j = (m + 1) / 2; j <= (m + 2) / 2; j++)
        {
            Q.push({i, j});
            M[i][j] = 1;
        }
    while (!Q.empty())
    {
        int nowX = Q.front().first;
        int nowY = Q.front().second;
        Q.pop();
        ans[M[nowX][nowY]]++;
        for (int i = 0; i < 4; i++)
        {
            int nextX = nowX + nx[i];
            int nextY = nowY + ny[i];
            int nextV = M[nowX][nowY] + 1;
            if (nextX < 1 || nextY < 1 || nextX > n || nextY > m)
                continue;
            if (M[nextX][nextY] != 0)
                continue;
            M[nextX][nextY] = nextV;
            Q.push({nextX, nextY});
        }
    }
    int oriAns = n / 2 + m / 2;
    for (auto it : ans)
    {
        if (it.second == 0)
            continue;
        int cnt = it.second;
        for (int i = 1; i <= cnt; i++)
            cout << oriAns << " ";
        oriAns++;
    }
    cout << endl;
}

C. Not Assigning

在这里插入图片描述
题目大意:
给你一颗树,让你给这个树的边赋权,使其任意2条连续边的和以及每条边是素数
思路:
已知奇数+奇数=偶数!=质数,因此边只能是2 3 2 3这种情况,而且不可能有一个点有超过2条边
在这里插入图片描述
如图所示,无法构成答案,对于可以构成的情况,我们使用dfs染色即可
代码:

void dfs(int x, int father, int color)
{
    for (int i = 0; i < g[x].size(); i++)
    {
        if (g[x][i] == father)
            continue;
        M[{x, g[x][i]}] = color;
        M[{g[x][i], x}] = color;
        dfs(g[x][i], x, ((color == 2) ? 3 : 2));
    }
}

void work()
{
    cin >> n;
    for (int i = 0; i <= n; i++)
        g[i].clear();
    M.clear();
    vector<pair<int, int>> V;
    for (int i = 1; i < n; i++)
    {
        int t1, t2;
        cin >> t1 >> t2;
        g[t1].push_back(t2);
        g[t2].push_back(t1);
        V.push_back({t1, t2});
    }
    bool ok = true;
    for (int i = 1; i <= n; i++)
        if (g[i].size() >= 3)
            ok = false;
    if (!ok)
    {
        cout << -1 << endl;
        return;
    }
    for (int i = 1; i < n; i++)
    {
        if (g[i].size() == 1)
        {
            dfs(i, 0, 2);
            break;
        }
    }
    for (auto it : V)
        cout << M[it] << " ";
    cout << endl;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Abmcar

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值