其他分享
首页 > 其他分享> > Codeforces Round #665 (Div. 2)

Codeforces Round #665 (Div. 2)

作者:互联网

目录

Codeforces Round #665 (Div. 2)

A. Distance and Axis

题意: 给你一个点a的坐标n,求点b的坐标,使得∣OB−∣AB∣∣=K∣OB−∣AB∣∣=K
题解: 先判断n和k的大小关系。b在OA中每移动一个点改变的大小为2,所以判断n−kn−k的奇偶性即可
代码:

#include <bits/stdc++.h>

using namespace std;

int main() {
    int t;
    cin >> t;
    while (t--) {
        int n, k;
        cin >> n >> k;
        if (n > k) {
            if ((n - k) % 2 == 0) cout << 0 << endl;
            else cout << 1 << endl;
        }
        else cout << abs(n - k) << endl;
    }
    return 0;
}

B. Ternary Sequence

题意: 有两个由0,1,2组成的数组,分别给你两个数组中0,1,2的个数,其中满足
20200822120518588.png
题解: 要让2分的尽可能多,然后让-2分的尽可能少,因此在取完2分的之后,让0分的尽可能多,这样2分的就尽可能少了
代码:

#include <bits/stdc++.h>

using namespace std;

typedef long long LL;

int main() {
    int T;
    cin >> T;
    while (T--) {
        LL x1, y1, z1, x2, y2, z2;
        cin >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
        LL res = 0;
        LL t1 = min(z1, y2);
        z1 -= t1;
        y2 -= t1;
        res += t1 * 2;
        LL t2 = min(y1, x2);
        y1 -= t2, x2 -= t2;
        LL t3 = min(y1, y2);
        y1 -= t3, y2 -= t3;
        LL t4 = min(x1, x2);
        x1 -= t4, x2 -= t4;
        LL t5 = min(x1, y2);
        x1 -= t5, y2 -= t5;
        LL t6 = min(x1, z2);
        x1 -= t6, z2 -= t6;
        res -= 2 * min(y1, z2);
        cout << res << endl;
    }
    return 0;
}

C. Mere Array

题意: 给你一个长度为n的数组a,其中gcd(ai,aj)=min(a) 的两两元素可以做交换,求最后能否将数组a改变成非递减的数组
题解: 把a数组排序后,如果不在最后位置的元素那么需要进行交换。由于任何两个数字都可以通过和最小值进行交换,完成这两个数字的交换。因此,如果不在最后位置的元素不是最小值的整数倍,那么就无法完成交换。
代码:

#include <bits/stdc++.h>

using namespace std;

int const N = 1e5 + 10;
typedef long long LL;
LL a[N], n, T, b[N];
LL INF = 1e18;

int main() {
    // int T;
    cin >> T;
    while (T--) {
        cin >> n;
        LL minv = INF;
        for (int i = 1; i <= n; ++i) scanf("%lld", &a[i]), minv = min(minv, a[i]);
        memcpy(b, a, sizeof a);
        sort(b + 1, b + 1 + n);
        // vector<LL> v;
        int flg = 1;
        for (int i = 1; i <= n; ++i) {
            if (a[i] == b[i]) continue;
            if (a[i] % minv) {
                flg = 0;
                break;
            }
        } 
        if (flg) cout << "YES\n";
        else cout << "NO\n";
        
    }
    return 0;
}

D. Maximum Distributed Tree

题意: 给定一棵n 个结点的树,对这棵树分配边权,使得这棵树的边权的乘积为k ,且要求所有两点的简单路径边权之和最大。k 以质因子的形式给出,有m 个质因子。结果取余1e9+7
题解: dfs计算出每条边被经过多少次,经过次数越多的分配的质数越大,即可得到最大值。要注意判断指数情况和边数不相等的情况。
代码:

#include <bits/stdc++.h>

using namespace std;

int const N = 1e5 + 10;
typedef long long LL;
LL a[N], n, e[N * 2], ne[N * 2], h[N], idx, m, zi[N], fazi[N], T;
LL INF = 1e18;
int mod = 1e9 + 7;
vector<LL> E;

void add(LL a, LL b) {
    e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}

LL dfs(int u, int fa)
{
    LL sum = 1;  // ans记录子树的最大值, sum记录子树点数和和u点
    for (int i = h[u]; ~i; i = ne[i])
    {
        int j = e[i];
        if (j == fa) continue;
        
        LL son = dfs(j, u);  
        sum += son;  // 更新sum
    }
    fazi[u] = max((LL)0, n - sum);
    zi[u] = sum;
    return sum;
}

int main() {
    cin >> T;
    while (T--) {
        cin >> n;
        memset(fazi, 0, sizeof fazi);
        E.clear();
        memset(zi, 0, sizeof zi);
        memset(h, -1, sizeof h);
        idx = 0;
        for (int i = 1; i <= n - 1; ++i) {
            LL a, b;
            scanf("%lld %lld", &a, &b);
            add(a, b), add(b, a);
        }
        dfs(1, -1);
        cin >> m;
        vector<LL> v;
        for (int i = 1; i <= m; ++i) {
            LL k;
            scanf("%lld", &k);
            v.push_back(k);
        }
        sort(v.begin(), v.end());
        reverse(v.begin(), v.end());
        for (int i = 2; i <= n; ++i) {
            E.push_back(zi[i] * fazi[i]);
        }
        sort(E.begin(), E.end());
        reverse(E.begin(), E.end());
        // cout << E.size() << endl;
        if (v.size() < E.size()) {
            while (v.size() < E.size()) v.push_back(1);
        }
        else if (v.size() > E.size()) {
            vector<LL> v3(v.begin(), v.begin() + v.size() - E.size() + 1);
            vector<LL> v2(v.begin() + v.size() - E.size() + 1, v.end());
            LL mul = 1;
            for (auto v3i: v3) mul = (mul * v3i ) % mod;
            v2.push_back(mul);
            sort(v2.begin(), v2.end());
            reverse(v2.begin(), v2.end());
            v = v2;
        }
        LL res = 0;
        for (int i = 0; i < E.size(); ++i) {
            res = (res + E[i] * v[i] % mod) % mod;
        }
        cout << res << endl;

    }
    return 0;
}

参考

标签:x1,min,int,665,Codeforces,cin,Div,y2,LL
来源: https://www.cnblogs.com/spciay/p/13546491.html