Codeforces Round #665 (Div. 2) 题解

A. Distance and Axis

解题思路:
简化公式可以得出:abs(2b - a) = k。若a >= 2b,则(n − k) / 2 = b。当n > k时,b为负数,不符合题意。答案就为为k - n。
示例代码:

#include <bits/stdc++.h>
using namespace std;
int main() {
    int t; 
	cin >> t; 
	while (t--) {
        int n,k; 
		cin >> n >> k;
        if (n < k) printf("%d\n", k - n);
        else if ((n - k) % 2 == 0) printf("0\n");
        else printf("1\n");
    }
    return 0;
}

B. Ternary Sequence

解题思路:
明显贪心,想要最大就要二和一尽量配对。b2和a0,a2配对。剩下的再和a1配对。
示例代码:

#include <bits/stdc++.h>
using namespace std;
int main() {
    int t; 
	cin >> t; 
	while (t--) {
        long long a, b, c, aa, bb, cc, ans = 0;
        cin >> a >> b >> c;
        cin >> aa >> bb >> cc;
        ans = min(bb, c) * 2ll;
        c = max(c - bb, 0ll);
        cc -= a + c;
        if (cc > 0) ans -= cc * 2ll;
        printf("%lld\n", ans);
    }
    return 0;
}



C. Mere Array

解题思路:
这道题可以用min来做交换。这样不用来考虑gcd(ai,aj) = min是否成立了。
示例代码:

#include <bits/stdc++.h>
using namespace std; 
int a[100005],b[100005];
int main() {
    int t; 
	cin >> t; 
	while (t--) {
        int n, Min = 0x7fffffff; 
		cin >> n;
        for (int i = 0; i < n; i++) 
			cin >> a[i], b[i] = a[i], Min = min(Min, a[i]);
        sort(a, a + n);
        bool flag = false;
        for (int i = 0; i < n; i++) {
            if (a[i] == b[i]) continue;
            else if (b[i] % Min != 0) {
                flag = true;
                break;
            }
        }
        if (!flag) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

D. Maximum Distributed Tree

解题思路:
dfs深搜。先用dfs求出每个边会经过几次。给每条边赋权值,就是后面的m个数。如果超过了,最大的权值要乘以后面的数。
示例代码:

#include <bits/stdc++.h>
using namespace std;
typedef vector<long long> vecl;
long long sz[100005], a[100005], p[100005];
vecl edge[100005];
int cntt = 0, n, m;
void dfs(int u, int pre) {
    sz[u] = 1;
    for (int i = 0; i < edge[u].size(); i++) {
        int v = edge[u][i];
        if (v != pre) {
            dfs(v, u);
            sz[u] += sz[v];
            a[cntt++] = (n - sz[v]) * sz[v];
        }
    }
}
int main() {
    int t; 
	cin >> t; 
	while (t--) {
        cin >> n;
        for (int i = 0; i <= n; i++) 
			edge[i].clear();
        for (int i = 0; i < n - 1; i++) {
            int u, v;
            cin >> u >> v;
            edge[u].push_back(v);
            edge[v].push_back(u);
        }
        cntt = 0;
        dfs(1, -1);
        sort(a, a + cntt);
        cin >> m;
        for (int i = 0; i < m; i++)
			cin >> p[i];
        sort(p, p + m);
        if (cntt > m) {
            for (int i = m; i < cntt; i++) p[i] = 1;
        } else {
            for (int i = cntt; i < m; i++) p[cntt - 1] = p[cntt - 1] * p[i] % 1000000007;
        }
        long long ans = 0;
        sort(p, p + cntt);
        for (int i = cntt - 1; i >= 0; i--) {
            ans = (ans + a[i] % 1000000007 * p[i]) % 1000000007;
        }
        printf("%lld\n", ans);
    }
    return 0;
}


E. Divide Square

解题思路:
线段树扫描,也可以用树状数组来做。正方形的范围就是1 ~ 1000005。接下来就可以做了。
示例代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int e6 = 1e6;
struct NODE {
    int t, l, r;
    bool friend operator < (NODE a, NODE b) {
        return a.t < b.t;
    }
};
NODE x[maxn << 1], y[maxn << 1];
int c[e6 + 10];
int lowbit(int x) {return x & -x;}
void add(int x, int val) {
    for (int i = x; i <= e6 + 5; i += lowbit(i)) {
        c[i] += val;
    }
}
int getsum(int x) {
    int sum = 0;
    for (int i = x; i >= 1; i -= lowbit(i)) {
        sum += c[i];
    }
    return sum;
}
int main() {
    int n, m;
    cin >> n >> m;
    long long ans = 1;
    for (int i = 0; i < n; i++) {
        int yy, lx, rx;
        cin >> yy >> lx >> rx;
        y[i << 1] = {lx, yy + 1, 1};
        y[i << 1 | 1] = {rx + 1, yy + 1, -1};
        if (lx == 0 && rx == e6) ans++;
    }
    for (int i = 0; i < m; i++) cin >> x[i].t >> x[i].l >> x[i].r;
    sort(y, y + 2 * n);
    sort(x, x + m);
    add(1, 1), add(e6 + 1, 1);
    int j = 0;
    for (int i = 0; i < m; i++) {
        while (j < n * 2 && y[j].t <= x[i].t) add(y[j].l, y[j].r), j++;
        ans += getsum(x[i].r + 1) - getsum(x[i].l) - 1;
    }
    printf("%lld", ans);
    return 0;
}


F.Reverse and Swap

解题思路:
这题看的我有点懵。我想拿二分做,搞了半天还是不太会。看了一些大佬的讲解也是没咋看懂。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值