cf992补题

cf992补题

总结

离上次打cf已经过了有一段时间了。之前那几场真是打的太差了,掉了很多分,而且正好最近期中考试需要复习,所以就好久都没有打了。之前估计也是隔三岔五打了。
昨天那一场cf992,比赛地址:https://codeforces.com/contest/2040,只写了abc,d题还是不会,今天看的题解。但是因为之前掉太多分了,没想到尽然加分了。

补题

A:Game of Division

  1. 做法:数据范围很小,只有100,可以做n^2的做法,直接暴力就可以了。只需要找到一个数,和其他数的差都不能被 k整除,就输出yes,和这个数的下标,不然输出no。
  2. 代码:
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
    int t;
    cin >> t;

    while (t -- ) {
        int n, k;
        cin >> n >> k;

        vector<int> a(n);
        for (int i = 0; i < n; i ++ ) {
            cin >> a[i];
        }

        int flag = -1;
        for (int i = 0; i < n; i ++ ) {
            int f2 = true;
            for (int j = 0; j < n; j ++ ) {
                if (i == j) {
                    continue;
                }
                int now = abs(a[i] - a[j]);
                if (now % k == 0) {
                    f2 = false;
                }
            }
            if (f2) {
                flag = i;
                break;
            }
        }

        if (flag != -1) {
            cout << "Yes" << endl << flag + 1 << endl;
        }
        else {
            cout << "No" << endl;
        }

    }

    return 0;
}

B:Paint a Strip

  1. 题意:首先对于这两种操作。第一种:将一个非零的位置,变为1,代价是1;第二种操作,当有一个l,r满足,l 和 r 处为1,并且区间中1的个数,大于等于区间长度的一半。可以将整个区间中所有 0 变为1,代价是0。对于给定的全零数组,将其变为全 1 的最小代价。
  2. 分析:需要代价最小,那么需要我们使用 操作1 的次数最少。这里有点类似于倍增的感觉。具体而言我们在1号位置进行一次 操作1,我们固定 操作2 的左端点为 位置1,然后之后的每次 操作1,我们都当作 操作2 的右端点。那么我们每次都要使得 操作2得到的区间尽量的长。假设当前已经有 k 个位置是1,然后操作二的右端点可以选为 2 * (k + 1)。
  3. 代码:
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
    int t;
    cin >> t;

    while (t -- ) {
        int n;
        cin >> n;

        if (n <= 2) {
            cout << n << endl;
            continue;
        }

        long long ans = 2, num = 4;
        while (num < n) {
            ans ++ ;
            num = num * 2 + 2;
        }

        cout << ans << endl;
    }

    return 0;
}

C:Ordered Permutations

  1. 题意:让S( p )最大的排列中,字典序为k的排列是哪一个。其中S( p )表示,所有子数组的最小值的和。比如序列p = [1 ,2 ,3],有子数组 [1], [2], [3], [1, 2], [1, 2, 3], [2, 3],。这六种。其S§ = 1 + 2 + 3 + 1 + 1 + 2 = 10。
  2. 分析:
    • 首先:对于一个长度为n的 排列p,其子数组的个数是一定的。
    • 其次:我们可以枚举子数组的最小值 x,那么p中,最小值是x 的子数组的个数是 num[x]。最终 S( p) = num[x] * x。我们要S( p)最大,那么贪心的想,当x越小,其num[x] 应该也要小。我们从 1 开始考虑,假设 放在了 idx 这个位置,那么 num[1] = idx * (n - idx + 1)。要num[1] 最小,那么idx = 1 或 n。也就是将 1 放在 两端。同理考虑 2 的时候,也有这样的形式。所以我们从 1 开始依次将每个数字放入数组中,只要放的位置,是当前空位置的两端,那么S( p)就是最大的。
    • 最后:对于上述这种放置的方法,一共有 2 ^ (n - 1)种,因为当前n - 1个元素放好之后,最后一个元素位置是固定的。
  3. 做法:从1开始 依次考虑 每个数字放的位置。假如把数字 x 放在后边,那么其最小的排列是 第 (n - x - 1) ^ 2。所以通过判断 k 和 (n - x - 1) ^ 2 的大小,来判断当前数字 x 是放在前边,还是后边。当 k > (n - x - 1) ^ 2 时,将 x 放在后边,并将 k 减去(n - x - 1) ^ 2。不然就将 x 放在前边,k不变。
  4. 值得一提的是,因为 n 可以取到 2e5 ,所以可能 计算 2 ^ (n - 1)会超过longlong,需要特判。
  5. 代码:代码中的dfs,可以暴力求出小范围的所有情况,作为参照。
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

vector<vector<int>> totala;

void dfs(vector<int> &a, int x, int y, int now) {
    if (x == y) {
        a[x] = now;
        totala.push_back(a);
        for (int x : a) {
            cout << x << " ";
        }
        cout << endl;
        return ;
    }

    a[x] = now;
    dfs(a, x + 1, y, now + 1);
    a[y] = now;
    dfs(a, x, y - 1, now + 1);
}

int main() {
    int t;
    cin >> t;

    while (t -- ) {
        long long n, k;
        cin >> n >> k;

//        long long q = k;

//        vector<int> tmp(n);
//        dfs(tmp, 0, n - 1, 1);

//        cout << endl;

        long long MX = 3e12;

        vector<long long> nums(n + 1, MX);
        nums[0] = 0, nums[1] = 1;
        for (int i = 2; i <= n; i ++ ) {
            nums[i] = 2 * nums[i - 1];
            if (nums[i] >= MX) {
                break;
            }
        }

        if (nums[n] < k) {
            cout << -1 << endl;
            continue;
        }

//        for (int x : totala[q - 1]) {
//            cout << x << " ";
//        }
//        cout << endl;

        vector<int> ans(n);
        for (int i = 1, x = 0, y = n - 1; i <= n; i ++ ) {
            if (k > nums[n - i]) {
                ans[y -- ] = i;
                k -= nums[n - i];
            }
            else {
                ans[x ++ ] = i;
            }
        }

//        for (int i = 0; i < n; i ++ ) {
//            if (ans[i] != totala[q - 1][i]) {
//                cout << "No" << endl;
//                break;
//            }
//        }
        for (int x : ans) {
            cout << x << " ";
        }
        cout << endl;
    }

    return 0;
}

D:Non Prime Tree

  1. 比赛的时候,第三题写太久了,第四题没什么时间了。赛后补题的时候,我的第一想法是可能要通过 度 和 素数来写。错的。之后就直接看的题解了,但是题解里之说了两种做法,而没有思路。我看懂了第一种做法,这里把我的理解写一写。
  2. 题意:将1 ~ 2n 的 n个数 赋值给树中节点,使得任意边的两个节点x,y,有abs(x - y) 不是素数。
  3. 做法:我们在 树上做一次dfs,依次将 1 ~ 2n,赋给遍历到的节点,如果存在当前节点 a 和其父节点 b,如果abs(a - b) = 2 或则 abs(a - b) 是个奇数,那么我们就将a节点的值 继续向上取。如a = 4, b = 2,那么让a 继续取得 5。abs(a - b) = 3,继续让a = 6。遍历结束后,就是满足条件的赋值了。
  4. 理解:首先对于每条边的两个端点 x,y,有abs(x - y) 不是素数。因为 1 不是素数,所以如果 x 和 y 是相邻的两个数,那么就可以满足条件。通过上述做法,一定会有两个节点其值是1 和 2,那么对于其他节点,最多会与其父节点发生2次冲突,也就是,也就是最终会使用 2 + 2(n - 2) = 2n - 2 小于 2n。所以一定能找到这样一个序列。这是我的理解,可能不对。
  5. 代码:
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
    int t;
    cin >> t;

    while (t -- ) {
        int n;
        cin >> n;

        vector<vector<int>> grid(n);
        for (int i = 0; i < n - 1; i ++ ) {
            int x, y;
            cin >> x >> y;
            x -- , y -- ;
            grid[x].push_back(y), grid[y].push_back(x);
        }

        vector<int> res(n);

        res[0] = 1;
        int idx = 2;

        auto dfs = [&](auto&& dfs, int now, int pre) -> void {
            for (int x : grid[now]) {
                if (x == pre) {
                    continue;
                }
                res[x] = idx;
                while (res[x] - res[now] != 1 &&
                    (res[x] % 2 != res[now] % 2 || res[x] - res[now] == 2)) {
                    res[x] ++ ;
//                    cout << x << " " << res[x] << endl;
                }
                idx = res[x] + 1;
                dfs(dfs, x, now);
            }
        };

        dfs(dfs, 0, -1);

        for (int x : res) {
            cout << x << " ";
        }
        cout << endl;
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值