力扣周赛84vp

75min AK,3 罚时。今天状态着实不太好。。。

A

模拟。

B

模拟。

C

暴力。写的很丑。

typedef unsigned long long ull;
class Solution {
  public:
    int largestOverlap(vector<vector<int>> &img1, vector<vector<int>> &img2) {
        int ans = 0;
        int n = img1.size();
        vector<ull> a, b;
        for (int i = 0; i < n; ++i) {
            ull x = 0;
            for (int j = 0; j < n; ++j) {
                x <<= 1;
                x |= img1[i][j];
            }
            a.push_back(x);
            x = 0;
            for (int j = 0; j < n; ++j) {
                x <<= 1;
                x |= img2[i][j];
            }
            b.push_back(x);
        }
        for (int i = 0; i < n; ++i) {
            vector<ull> aa = a, bb = b;
            for (auto &x : aa)
                x >>= i;
            for (int j = 0; j < n; ++j) {
                int tot = 0;
                for (int k = 0; k + j < n; ++k) {
                    ull x = (aa[k] & b[k + j]);
                    tot += __builtin_popcountll(x);
                }
                ans = max(ans, tot);
                tot = 0;
                for (int k = 0; k + j < n; ++k) {
                    ull x = (aa[k + j] & b[k]);
                    tot += __builtin_popcountll(x);
                }
                ans = max(ans, tot);
            }
            for (auto &x : bb)
                x >>= i;
            for (int j = 0; j < n; ++j) {
                int tot = 0;
                for (int k = 0; k + j < n; ++k) {
                    ull x = (bb[k] & a[k + j]);
                    tot += __builtin_popcountll(x);
                }
                ans = max(ans, tot);
                tot = 0;
                for (int k = 0; k + j < n; ++k) {
                    ull x = (bb[k + j] & a[k]);
                    tot += __builtin_popcountll(x);
                }
                ans = max(ans, tot);
            }
        }
        return ans;
    }
};

D

dfs,一个数组记录子树内的距离和,另一个数组记录父亲方向的距离和,两个加起来就好了。

但是这题的数据范围是假的,说了 1 <= N <= 10000 结果用例 69:N = 11978。。。。。。

const int maxn = 1e5 + 9;
class Solution {
  public:
    int sz[maxn], w[maxn], ans[maxn], d[maxn];
    vector<int> g[maxn];
    void dfs(int u, int f) {
        sz[u] = 1;
        for (auto v : g[u]) {
            if (v == f)
                continue;
            dfs(v, u);
            sz[u] += sz[v];
            w[u] += w[v];
        }
        w[u] += sz[u] - 1;
    }
    void dfs1(int u, int f, int n) {
        if (f >= 0) {
            d[u] = d[f] + w[f] - w[u] - sz[u] + n - sz[u];
        } else {
            d[u] = 0;
        }
        for (auto v : g[u]) {
            if (v == f)
                continue;
            dfs1(v, u, n);
        }
    }
    vector<int> sumOfDistancesInTree(int n, vector<vector<int>> &edges) {
        memset(sz, 0, sizeof(int) * n);
        memset(w, 0, sizeof(int) * n);
        memset(ans, 0, sizeof(int) * n);
        for (auto &x : edges) {
            g[x[0]].push_back(x[1]);
            g[x[1]].push_back(x[0]);
        }
        dfs(0, -1);
        dfs1(0, -1, n);
        vector<int> res;
        for (int i = 0; i < n; ++i) {
            ans[i] = d[i] + w[i];
            res.push_back(ans[i]);
        }
        return res;
    }
};
posted @ 2021-10-10 23:48  Theophania  阅读(27)  评论(0)    收藏  举报