2020 Multi-University Training Contest 2


Total Eclipse · 并查集

http://acm.hdu.edu.cn/showproblem.php?pid=6763
在这里插入图片描述

相当于,往一个连通块里加入一个小的值 b x b_x bx,都需要将该连通块里所有的点权全部减到 b x b_x bx,然后 x x x 成为新的连通块的 r o o t root root ,其中每次操作的代价 = b[ 连通块原来的root ] - b[x]

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e5 + 10;
vector<int> e[N];
int n, m;

ll b[N], res;
int id[N], vis[N];
int fa[N];

int find(int x) {
    return fa[x] == x ? x : fa[x] = find(fa[x]);
}

void Union(int x, int y) {
    int fy = find(y);
    if (x == fy) return;
    res += b[fy] - b[x];
    fa[fy] = x;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int T;
    cin >> T;
    while (T--) {
        cin >> n >> m;
        for (int i = 1; i <= n; i++) {
            cin >> b[i];
            fa[i] = id[i] = i;
            vis[i] = 0;
            e[i].clear();
        }

        sort(id + 1, id + 1 + n, [](int x, int y) {
            return b[x] > b[y];
        });
        
        for (int i = 1, u, v; i <= m; i++) {
            cin >> u >> v;
            e[u].push_back(v);
            e[v].push_back(u);
        }

        res = 0;
        for (int i = 1; i <= n; i++) {
            vis[id[i]] = 1;

            int u = id[i];
            for (int v:e[u]) {
                if (vis[v]) {
                    Union(u, v);
                }
            }
        }
        // 多连通块时统计所有的 b[root]
        for (int i = 1; i <= n; i++) {
            if (find(i) == i) {
                res += b[i];
            }
        }
        cout << res << endl;
    }
    return 0;
}

Blood Pressure Game
1003 Count on a Tree II Striking Back
1004 Diamond Rush
1005 New Equipments

The Oculus · hash

http://acm.hdu.edu.cn/showproblem.php?pid=6768
在这里插入图片描述

斐波那契数模任何数后都有循环节,只不过循环节有长有短,
即找到一个 P P P,使得一个循环节的长度能覆盖 F 1 . . . F 2 e 6 + 1 F_1...F_{2e6+1} F1...F2e6+1
经测试满足要求的 P ≥ 1 0 12 + 9 P\ge10^{12}+9 P1012+9,一个使用乘法会爆 long long 的范围

要么 P P P 值取较小值,即 F i F_i Fi 必定会有冲突,需要至少两个hash来存
要么取 unsinged long long ,自然溢出就是 F i F_i Fi,可以保证任意两个都不相同

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int N = 2e6 + 10;
const int M = 2e6 + 1;
int n;
unordered_map<ull, int> mp;
ull F[N];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    F[0] = F[1] = 1;
    mp[1] = 1;
    for (int i = 2; i <= M; i++) {
        F[i] = F[i - 1] + F[i - 2];
        mp[F[i]] = i;
    }

    int T;
    cin >> T;
    ull A, B, C;
    while (T--) {
        A = B = C = 0;

        cin >> n;
        for (int i = 1, x; i <= n; i++) {
            cin >> x;
            if (x) A += F[i];
        }

        cin >> n;
        for (int i = 1, x; i <= n; i++) {
            cin >> x;
            if (x) B += F[i];
        }
        
        cin >> n;
        for (int i = 1, x; i <= n; i++) {
            cin >> x;
            if (x) C += F[i];
        }

        ull Fk = A * B - C;
        cout << mp[Fk] << endl;
    }

    return 0;
}

1007 In Search of Gold
1008 Dynamic Convex Hull
1009 It’s All Squares

Lead of Wisdom

http://acm.hdu.edu.cn/showproblem.php?pid=6772
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Cloth {
    ll a, b, c, d;
};
vector<Cloth> e[55], E[55];
int n, k;
ll res = 0;
int tot = 0;

void dfs(int u, ll A, ll B, ll C, ll D) {
    if (u == tot + 1) {
        res = max(res, A * B * C * D);
        return;
    }

    for (int i = 0, lim = E[u].size(); i < lim; i++) {
        dfs(u + 1, A + E[u][i].a, B + E[u][i].b, C + E[u][i].c, D + E[u][i].d);
    }
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int T;
    cin >> T;
    while (T--) {
        res = 0;
        tot = 0;
        cin >> n >> k;
        for (int i = 1, type, a, b, c, d; i <= n; i++) {
            cin >> type >> a >> b >> c >> d;
            if (!a && !b && !c && !d) continue;
            e[type].push_back({a, b, c, d});
        }

        for (int i = 1; i <= k; i++) {
            if (e[i].size()) {
                E[++tot] = e[i];
            }
        }

        dfs(1, 100, 100, 100, 100);
        cout << res << endl;

        for (int i = 1; i <= k; i++) {
            e[i].clear();
        }
    }
    return 0;
}

1011 King of Hot Pot

String Distance · lcs

http://acm.hdu.edu.cn/showproblem.php?pid=6774
在这里插入图片描述

#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 10;
int n, m;
string A, B;
int f[N][25], g[N][26];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int T, q, l, r;
    cin >> T;
    while (T--) {

        cin >> A >> B;
        A = " " + A;
        B = " " + B;

        n = A.length() - 1;
        //g[i][j] 表示 字母j在A[i....n]里出现的最早下标
        for (int i = 0; i < 26; i++) {
            g[n + 2][i] = g[n + 1][i] = n + 1;
            // 注意边界 配合后面的 g[f[i - 1][j - 1] + 1][B[i] - 'a']
        }
        for (int i = n; i; i--) {
            for (int j = 0; j < 26; j++) {
                g[i][j] = g[i + 1][j];
            }
            g[i][A[i] - 'a'] = i;
        }

        m = B.length() - 1;
        cin >> q;
        while (q--) {
            cin >> l >> r;
            // f[i][j]表示B[1...i]与A[l..r]能匹配的公共序列长度为j时 A的最后一个匹配上的位置
            // init
            for (int i = 0; i <= m; i++) {
                for (int j = 0; j <= m; j++) {
                    f[i][j] = r + 1;
                }
            }

            for (int i = 0; i <= m; i++) {
                f[i][0] = l - 1; // 设置初始位置 从i=0开始初始化
                for (int j = 1; j <= i; j++) {
                    f[i][j] = min(f[i][j], f[i - 1][j]);
                    f[i][j] = min(f[i][j], g[f[i - 1][j - 1] + 1][B[i] - 'a']);
                }
            }
            int lcs = 0;
            for (int i = 1; i <= m; i++) {
                for (int j = 1; j <= m; j++) {
                    if (f[i][j] <= r) {
                        lcs = max(lcs, j);
                    }
                }
            }
            // ans= |A|+|B|-2lcs(A,B)
            cout << (r - l + 1) + m - 2 * lcs << endl;
        }
    }
    return 0;
}
©️2020 CSDN 皮肤主题: 像素格子 设计师:CSDN官方博客 返回首页