10.6 国庆中秋 提高组 热身赛改错

· · 个人记录

栅栏密码 原题

找规律,发现当 h=3时,顺序的第一五九个字符变成了第一二三个,两个字符相差2*(h-1)最后一行和第一行一样

第二行有俩种情况:

所以跟据规律可以得出:当在第一和h行时,前一个与后一个字符相差2*(h-1),第二行时奇数个为2*(h-i),第偶数个时为2*(i-1)。

#include<bits/stdc++.h>
using namespace std;
string s;
char ans[100080];
int han = 0;
int h, len, pd1, pd2;
int check() {
    if (han == 1 || han == h)return pd1 + (h - 1) * 2;
    if (pd2 % 2 == 1)return pd1 + (h - han) * 2;
    else return pd1 + 2 * (han - 1);
}
int main() {
    cin >> h >> s;
    len = s.size();
    s = " " + s;
    han = pd1 = pd2 = 1;
    for (int i = 1; i <= len; i++) {
        ans[pd1] = s[i];
        pd1 = check();
        pd2++;
        if (pd1 > len) {
            han++;
            pd1 = han;
            pd2 = 1;
        }
    }
    for (int i = 1; i <= len; i++)
        cout << ans[i];
    return 0;
}

旅行计划 原题

在有向无环图中求经过点最多路线的点数。

第一次想用递推,因为是有向图,所以可以得出后一个点的点数为前一个点的点数加一。但是发现有些样例的答案会小。重新读题发现第i个点可能在i-1的西边,计算顺序会少一个。

考虑动态规划: 先用数组记录入度为0的点获取最先到达的城市。再根据它的道路扩展其他城市的f[i].

状态转移方程:$ f[i]=\max{f[j]} +1$, $j$ 为 $i$ 的前一个点。 ```cpp #include <bits/stdc++.h> using namespace std; const int N = 1e5+5; int n, m, indu[N], x, y, f[N]; vector<int>g[N]; queue<int> q; int main() { cin >> n >> m; for (int i = 1; i <= m; i++) { cin >> x >> y; g[x].push_back(y);//存图 indu[y]++;//寻找入度为0的点作为起点 } for (int i = 1; i <= n; i++) if (indu[i] == 0) q.push(i);//起点入队 while (!q.empty()) { int u = q.front(); q.pop(); for (auto it : g[u]) { if (f[it] < f[u] + 1) f[it] = f[u] + 1;//更新f[i]值 indu[it]--; if (indu[it] == 0) q.push(it); } } for (int i = 1; i <= n; i++) cout << f[i] + 1 << endl;//起点也算一个城市 return 0; } ``` ## 商务旅行 [原题](https://www.luogu.com.cn/problem/P8855) 一颗 $N$ 个节点的树,所有边权为 $1$ ,给定一个路线,求路线的最小值。 考试的时候想用全源最短路$(Dijkstra,Bellman-Ford,SPFA)$求出每个点到其他点的最短距离,但是显而易见炸空间 ~~还有不会写~~ 求树上两个点的最短距离,可以用**树状差分**,用各自的深度减去最近公共祖先的深度再相加即可求得距离。定义 $dis[i][j]$ 为 $i$ 到 $j$ 的最小值,$dep[i]$ 为 $i$ 的深度, $lca(i,j)$ 为 $i$ 和 $j$ 的最近公共祖先。 计算公式: $ dis[i][j] = dis[ lca(i,j) ][i] + dis[lca(i,j)][j] = dep[i] - dep[lca(i,j)] + dep[j] - dep[lca(i,j)]
#include<bits/stdc++.h>
using namespace std;
int ans, a, b, n, m, dep[1000005], fa[1000005], dp[1000005];
vector<int>edge[1005];
void dfs(int x, int father) {
    fa[x] = father;
    for (auto it : edge[x]) {
        if (it != father) {
            dep[it] = dep[x] + 1;
            dfs(it, x);
        }
    }
}
int lca(int x, int y) {
    while (dep[x] > dep[y])
        x = fa[x];
    while (dep[y] > dep[x])
        y = fa[y];
    while (x != y) {
        x = fa[x];
        y = fa[y];
    }
    return x;
}
int main() {
    cin >> n;
    for (int i = 1; i < n; i++) {
        cin >> a >> b;
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    dfs(1, 0);
    cin >> m >> b;
    ans += dep[1] - dep[lca(1, b)] + dep[b] - dep[lca(1, b)];
    a = b;
    for (int i = 2; i <= m; i++) {
        cin >> b;
        ans += dep[a] - dep[lca(a, b)] + dep[b] - dep[lca(a, b)];
        a = b;
    }
    cout << ans;
    return 0;
}

鬼子进村 原题

给定 N 个房子,对房子进行查询,修改,删除操作,其中查询时,求房子与它相连的房子个数。因为 N\leq 5×10^5,所以时间复杂度O(N) 或 O(N \log N)

考试时看到查询,修改,删除就想到线段树了,但是没有打出来。所以先打了一个暴力代码

#include<bits/stdc++.h>
using namespace std;
const int N = 5e4+5;
int n, m, x, vis[N], ti = 1;
char c;
int main() {
    cin >> n >> m;
    while (m--) {
        cin >> c;
        if (c == 'D') {
            cin >> x;
            vis[x] = ti;
            ti++;
        }
        if (c == 'R') {
            int mx = -1e9;
            for (int i = 1; i <= n; i++)
                mx = max(mx, vis[i]);
            for (int i = 1; i <= n; i++) {
                if (vis[i] == mx) {
                    vis[i] = 0;
                    break;
                }
            }
        }
        if (c == 'Q') {
            cin >> x;
            int ls = 0, rs = 0;
            if (vis[x]) {
                cout << 0 << endl;
                continue;
            }
            for (int i = x - 1; i >= 1; i--) {
                if (vis[i]) break;
                ls++;
            }
            for (int i = x + 1; i <= n; i++) {
                if (vis[i]) break;
                rs++;
            }
            cout << ls + 1 + rs << endl;
        }
    }
    return 0;
}

要维护每个点的状态和查询连通块大小,可以使用set维护点的状态,因为可以直接使用函数求。再用一个栈维护修复的点。如果一个点被摧毁,那它的最大连通块为0。

#include<bits/stdc++.h>
using namespace std;
const int N = 5e4+5;
int n, m, a[N], x;
char op;
stack<int>sta;
set<int>st;
int main() {
    cin >> n >> m;
    st.insert(0), st.insert(n + 1); //从1到n,方便计算
    for (int i = 1; i <= m; i++) {
        cin >> op;
        if (op == 'D') {
            cin >> x;
            sta.push(x);
            st.insert(x);
        }
        if (op == 'R') {
            int t = sta.top();
            sta.pop();
            st.erase(t);
        }
        if (op == 'Q') {
            cin >> x;
            auto t = lower_bound(st.begin(), st.end(), x); //查询第一个大于等于x的数
            if (*t == x) //x在st中,已经被摧毁,直接输出0
                cout << "0" << endl;
            else
                cout << *t - *(--t) - 1 << endl;
        }
    }
    return 0;
}

无线传输 原题

给定一个字符串s1,求一个字符串s2,使得重复连接s2,其字串可以得到s1。

因为s1由s2重复连接构成,所以s1具有周期性。用 KMP算法求最小周期长度。

时间复杂度:O(N)

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+5;
char s[N], t[N];
int nxt[N], lens, lent, n;
int main() {
    cin >> n >> s + 1;
    for (int i = 2, j = 0; i <= n; i++) {
        while (j && s[j + 1] != s[i]) j = nxt[j] ;
        if (s[j + 1] == s[i]) j++;
        nxt[i] = j;
    }
    cout << n - nxt[n];
    return 0;
}