跳到正文

个人记录

10.18模拟赛总结

zsc2013约 7 分钟
原文
文章信息与版本

文章信息

发布
2025年10月18日
收录
2025年12月14日
更新
2026年11月18日
篇幅
约 7 分钟

版本

  1. v2当前
  2. v1

10.18模拟赛总结

估分:

预估:

T1:0; T2:100; T3:100; T4:0; T5:0。

实际:

T1:0; T2:100; T3:10; T4:0; T5:0.

分析:

T1:无。 T2:无。 T3:为了在std::string的头部插入字符而使用了insert函数,但是没有考虑其本身 O(n)O(n) 的时间复杂度,导致在部分 n≤106n \le 10^6 的点上超时。 T4:无; T5:无。

题解:

T1:

思路:

不会

代码:
C++
// 没写出来

T2:

思路:

显然,我们可以用按下开关的方式“移动”关闭的灯,两个相邻的关闭的灯可以一起打开。

因此判断关闭的灯的数量是否为偶数即可。

(我闲的没事求了连通块大小,我在干嘛……)

代码:
C++
#include <bits/stdc++.h>
using namespace std;
struct p
{
    int x, y, tag;
};
int T, n, m, a[2005][2005], dmove[2][4] = {1, 0, -1, 0, 0, 1, 0, -1}, cnt;
bool vis[2005][2005];
void bfs(int x, int y, int ncol)
{
    queue<p> q;
    q.push({x, y, ncol});
    vis[x][y] = 1;
    while (!q.empty())
    {
        p now = q.front();
        q.pop();

        cnt++;
        for (int i = 0; i <= 3; i++)
        {
            int tar_x = now.x + dmove[1][i], tar_y = now.y + dmove[0][i];
            if (a[tar_x][tar_y] == 0 && !vis[tar_x][tar_y]) q.push({tar_x, tar_y, -now.tag}), vis[tar_x][tar_y] = 1;;
        }
    }
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    // freopen("light.in", "r", stdin);
    // freopen("light.out", "w", stdout);
    cin >> T;
    while (T--)
    {
        cnt = 0;
        memset(vis, 0, sizeof(vis));
        memset(a, 0xff, sizeof(a));
        cin >> n >> m;
        for (int i = 1; i <= n; i++)
        {
            char c;
            for (int j = 1; j <= m; j++) cin >> c, a[i][j] = c - '0';
        }
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= m; j++)
            {
                if (!vis[i][j] && a[i][j] == 0) bfs(i, j, 1);
            }
        }
        if (cnt % 2 == 0) cout << "Yes" << '\n';
        else cout << "No" << "\n";
    }

    return 0;
}

T3:

思路:

显然的,所有的城市构成了一棵树。

结合字典序的定义,我们可以每到一个点就访问它所有可能的字典序更大的子节点。 每个新的子节点可能有以下情况 1. 比之前看过的最大标识小: 此时,当前节点一定不可能在最大字典序所在的路径上,因此忽略当前节点及之后的可能 。 2. 不比之前看过的最大标识小: 此时,当前节点可能是字典序最大的路径上的节点,我们先将选择当前节点,但之后可能会被其他点覆盖。

最终,将所有可能的最大节点链接起来即可。

实现:

为了优化,我们在输入时就预处理出每个节点标识的子节点的最大标识。 接着,我们 dfs 一遍,预处理出所有点的深度。 最后按思路 bfs 即可,每次只需访问标识最大的子节点。

代码:
C++
// 因为某些不知名原因只得了96pts
#include <bits/stdc++.h>
using namespace std;
int n, dep[1000005];
char val[1000005];
vector<int> v[1000005];
char mxchar[1000005];
void dfs(int now)// 处理深度
{
    for (int i : v[now])
    {
        dep[i] = dep[now] + 1;
        dfs(i);
    }
    return ;
}
string bfs(int u)// 计算答案
{
    char ans[1000005];// 每个深度可能的答案
    memset(ans, 0, sizeof(ans));
    queue<int> q;
    q.push(u);
    while (!q.empty())
    {
        int now = q.front();
        q.pop();
        if (ans[dep[now] - 1] > val[now]) continue;// 选
        else ans[dep[now] - 1] = val[now];// 放弃之前的并选当前的
        for (int i : v[now])
        {
            if (val[i] == mxchar[now]) q.push(i);
        }
    }
    return ans;
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    // freopen("road.in", "r", stdin);
    // freopen("road.out", "w", stdout);
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> val[i];
    for (int i = 1; i < n; i++)
    {
        int pu, pv;
        cin >> pu >> pv;
        v[pu].push_back(pv);
        // if (val[pv] > mxchar)
        mxchar[pu] = max(mxchar[pu], val[pv]);// 最大标识
    }
    dep[1] = 1;
    dfs(1);
    cout << bfs(1);
    return 0;
}

T4、T5:

不会。

原文