• Codeforces Round #544 (Div. 3)简单题解


    复健,时间有限题解比较简陋


    A. Middle of the Contest

    将小时转成分钟,得到起止时间在一天中的分钟数,取平均值即可,复杂度O(1)。平均值转换会时间的时候注意前导0。

    void solve(int x) {
        x /= 2;
        printf("%02d:%02d
    ", x / 60, x % 60);
    }
    int main() {
        // freopen("in.txt", "r", stdin);
        // ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)cpp;
        int h1, h2, m1, m2;
        char c;
        cin >> h1 >> c >> m1;
        cin >> h2 >> c >> m2;
        solve(h1 * 60 + m1 + h2 * 60 + m2);
    }
    

    B. Preparation for International Women’s Day

    要加起来能被k整除, 只需要看模k的余数即可。余数为i的与余数为k-i的互补可以被k整除,通过计数看有多少对能互补。需要注意的是,为余数为0k/2(k为偶数)时,只能同余数的互补,此时计数是偶数个时都能配对,奇数个时能配对的数量是计数 - 1。复杂度O(n + k)

    int main() {
        // freopen("in.txt", "r", stdin);
        ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
        int n, k, x, cnt = 0;
        cin >> n >> k;
        int d[k] = {0};
        for (int i = 0; i < n; ++i) cin >> x, d[x % k]++;
        for (int i = 0; i < (k + 1) / 2; ++i) {
            if (i == 0)
                cnt += d[i] / 2;
            else {
                cnt += min(d[i], d[k - i]);
            }
        }
        if (k % 2 == 0) cnt += d[k / 2] / 2;
        cout << cnt * 2;
    }
    

    C. Balanced Team

    单调队列,从小到大添加元素,保证队首和队尾差不超过5,超过了则出队,否则用当前队列大小更新最优解。如果元素x < yx一定比y先入队,而且能与x共存的最小值sx和能与y共存的最小值sysx <= sy。使用单调队列,每次入队后进行出队操作,出队完成后队首就是能与入队元素共存的最小值,队列内的元素就是以入队元素为最大值时所有能存在的元素。复杂度O(n)

    int main() {
        // freopen("in.txt", "r", stdin);
        ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
        int n;
        cin >> n;
        ll a[n + 1];
        for (int i = 1; i <= n; ++i) cin >> a[i];
        sort(a + 1, a + 1 + n);
        int cnt = 0, l = 1, r = 2;
        while (l <= r && r <= n) {
            if (a[r] - a[l] > 5) cnt = max(cnt, r - l), l++;
            r++;
        }
        cnt = max(cnt, r - l);
        cout << cnt;
    }
    

    D. Zero Quantity Maximization

    d * a[i] + b[i] = 0可得d = - b[i] / a[i],统计每种d取值的个数,取最大即可。对于a[i]0的情况需要特殊讨论,如果b[i]也为0则此时d可以取任意值;否则,d的取值为0。另外,为了避免浮点误差,不能直接统计d,而是要统计<a, b>这个配对;同时,为了归一化,需要将ab同时除以他们的最大公约数,并保证a是正数。复杂度O(nlog(n)),log是因为用了map来计数。

    // Author : RioTian
    // Time : 20/11/10
    #include <bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int N = 1e5 + 10;
    int main() {
        // freopen("in.txt", "r", stdin);
        ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
        int n;
        cin >> n;
        ll a[n + 1], b[n + 1];
        for (int i = 0; i < n; ++i) {
            cin >> a[i];
        }
        for (int i = 0; i < n; ++i) {
            cin >> b[i];
        }
        int zeroBCnt = 0, zeroBothCnt = 0;
        map<pair<int, int>, int> hash;
        for (int i = 0; i < n; ++i) {
            if (a[i] == 0) {
                if (b[i] == 0) ++zeroBothCnt;
                continue;
            }
            if (b[i] == 0) {
                ++zeroBCnt;
            }
            int divisor = gcd(abs(a[i]), abs(b[i]));
            a[i] /= divisor;
            b[i] /= divisor;
            if (a[i] < 0) {
                a[i] = -a[i];
                b[i] = -b[i];
            }
            if (hash[make_pair(a[i], b[i])])
                ++hash[make_pair(a[i], b[i])];
            else
                hash[make_pair(a[i], b[i])] = 1;
        }
        int ans = zeroBCnt;
        for (auto item : hash) {
            if (item.second > ans) ans = item.second;
        }
        cout << ans + zeroBothCnt << endl;
        return 0;
    }
    

    E. K Balanced Teams

    与C题思路类似,先得到取每个元素为最大值,能共存的元素有哪些(排过序的数组保留首位指针即可),比如位置为i的元素最小可共存元素的位置是maxStart[i],这样数组里面maxStart[i]i都是可共存元素。问题就转成如何在里面选k个,让元素尽量多,这样dp即可。dp[i][j]表示前i个元素选j队的最优值,则如果选maxStart[i]i,最优值为dp[maxStart[i] - 1], j - 1] + i - maxStart[i] + 1;如果不选,最优值为dp[i - 1][j];两者取最优得到状态转移方程。另外由于j只会从j - 1转移,因此可以用滚动数组节约内存。复杂度O(nk)

    // Author : RioTian
    // Time : 20/11/10
    #include <bits/stdc++.h>
    using namespace std;
    const int N = 5e3 + 10;
    int a[N], maxStart[N], dp[N][2];
    int n, k;
    int main() {
        cin >> n >> k;
        for (int i = 0; i < n; ++i) cin >> a[i];
        sort(a, a + n);
        int l = 0, r = 0;
        while (r < n) {
            if (a[r] - a[l] <= 5) {
                maxStart[r] = l, ++r;
                continue;
            }
            ++l;
        }
        for (int i = 1; i <= k; ++i) {
            for (int j = 0; j < n; ++j) {
                if (maxStart[j]) {
                    dp[j][i % 2] =
                        max(dp[j - 1][i % 2],
                            dp[maxStart[j] - 1][(i - 1) % 2] + j - maxStart[j] + 1);
                    continue;
                }
                dp[j][i % 2] = max(dp[j - 1][i % 2], j - maxStart[j] + 1);
            }
        }
        cout << dp[n - 1][k % 2] << endl;
        return 0;
    }
    

    F1. Spanning Tree with Maximum Degree

    直接找到度最大的节点bfs即可,复杂度O(n + m)

    #include <iostream>
    #include <queue>
    #include <vector>
    
    using namespace std;
    
    vector<int> node[200010];
    bool visited[200010];
    
    void bfs(int start) {
        queue<int> qu;
        qu.push(start);
        visited[start] = true;
        while (qu.size()) {
            int cur = qu.front();
            qu.pop();
            for (auto next : node[cur]) {
                if (visited[next]) continue;
                visited[next] = true;
                qu.push(next);
                cout << cur + 1 << ' ' << next + 1 << endl;
            }
        }
    }
    
    int main() {
        int n, m, x, y, tmp, maxCnt = 0, maxNode = -1;
        cin >> n >> m;
        for (int i = 0; i < m; ++i) {
            cin >> x >> y;
            --x;
            --y;
            node[x].push_back(y);
            node[y].push_back(x);
            tmp = node[x].size() > node[y].size() ? x : y;
            if (node[tmp].size() > maxCnt) {
                maxCnt = node[tmp].size();
                maxNode = tmp;
            }
        }
        bfs(maxNode);
        return 0;
    }
    

    F2. Spanning Tree with One Fixed Degree

    如果从1的一个分支出发能从另一个分支回到1,则这些分支划分为同一组,dfs即可得到这些分组。如果一组里面所有分支都被去掉了,则这组里面的节点就无法出现在树里面,因此至少要保留一个。dfs得到有多少这样的组,每组里面取一个分支,剩下还可以取则任意取。这些分支作为bfs的第一步,继续搜下去,按搜索顺序输出即可。非法的情况有:dfs时存在节点没有走到;需要的度数比组数少(此时至少有一组所有分支都被去掉);需要的度数比1链接的分支多。复杂度O(n + m)

    #include <cstring>
    #include <iostream>
    #include <queue>
    #include <vector>
    
    using namespace std;
    bool visited[200010];
    vector<int> node[200010];
    vector<int> group[200010];
    queue<int> qu;
    int n, m, d, g;
    
    void dfs(int cur, int parent) {
        visited[cur] = true;
        for (auto next : node[cur]) {
            if (visited[next]) {
                if (parent == -1) group[g - 1].push_back(next);
                continue;
            }
            if (parent == -1) {
                group[g++].push_back(next);
            }
            dfs(next, cur);
        }
    }
    
    void bfs() {
        while (qu.size()) {
            int cur = qu.front();
            qu.pop();
            for (auto next : node[cur]) {
                if (!visited[next]) {
                    visited[next] = true;
                    cout << cur + 1 << ' ' << next + 1 << endl;
                    qu.push(next);
                }
            }
        }
    }
    
    int main() {
        int x, y;
        cin >> n >> m >> d;
        for (int i = 0; i < m; ++i) {
            cin >> x >> y;
            --x;
            --y;
            node[x].push_back(y);
            node[y].push_back(x);
        }
        memset(visited, 0, sizeof(visited));
        dfs(0, -1);
        for (int i = 0; i < n; ++i) {
            if (!visited[i]) {
                cout << "NO" << endl;
                return 0;
            }
        }
        if (g > d || node[0].size() < d) {
            cout << "NO" << endl;
            return 0;
        }
        cout << "YES" << endl;
        memset(visited, 0, sizeof(visited));
        visited[0] = true;
        d -= g;
        for (int i = 0; i < g; ++i) {
            cout << '1' << ' ' << group[i][0] + 1 << endl;
            visited[group[i][0]] = true;
            qu.push(group[i][0]);
            for (int j = 1; d && j < group[i].size(); ++j) {
                cout << '1' << ' ' << group[i][j] + 1 << endl;
                visited[group[i][j]] = true;
                qu.push(group[i][j]);
                --d;
            }
        }
        bfs();
        return 0;
    }
    

    The desire of his soul is the prophecy of his fate
    你灵魂的欲望,是你命运的先知。

  • 相关阅读:
    学习数据结构基础
    epoll
    pthread_create
    设置套接字选项
    5中I/O模型
    数据仓库一些整理(列式数据库)
    mysql分区方案的研究
    订单表的分库分表方案设计(大数据)
    从源码角度理清memcache缓存服务
    性能,不是不重要,而是,它没有可维护性重要
  • 原文地址:https://www.cnblogs.com/RioTian/p/13956281.html
Copyright © 2020-2023  润新知