周赛补题

leetcode :

第一题icon-default.png?t=M85Bhttps://leetcode.cn/problems/determine-if-two-events-have-conflict/ 比赛时把字符串转换成数字来做的,其实没有必要,因为题目只要求返回 bool 值,直接用字符串来比较大小就可以

class Solution {
public:
    bool haveConflict(vector<string>& event1, vector<string>& event2) {
        return event1[1] >= event2[0] && event1[0] <= event2[1];
    }
};

第二题icon-default.png?t=M85Bhttps://leetcode.cn/problems/number-of-subarrays-with-gcd-equal-to-k/

开始没看到数据范围。。可以直接暴力枚举每一个子数组的 

class Solution {
public:
    int subarrayGCD(vector<int>& nums, int k) {
        int n = nums.size();
        int ans = 0;

        for(int i = 0; i < n; i ++){
            if(nums[i] % k != 0)  continue;
            int x = 0;

            for(int j = i; j < n; j ++){
                x = gcd(x, nums[j]);
                if(x == k)  ans ++;
            }
        }

        return ans;
    }
};

第三题icon-default.png?t=M85Bhttps://leetcode.cn/problems/minimum-cost-to-make-array-equal/description/ 中位数贪心,把 cost[i] 理解成 nums[i] 的出现次数,把所有数变成中位数是最优的

详细证明看看 leetcode 462

class Solution {
public:
    long long minCost(vector<int>& nums, vector<int>& cost) {
        vector<pair<int, int>> res;
        long long sum = 0;
        for(int i = 0; i < nums.size(); i ++){
            res.push_back({nums[i], cost[i]});
            sum += cost[i];
        }

        sort(res.begin(), res.end());
        long long mid = sum / 2;
        long long zhon;

        long long ans = 0, count1 = 0;
        for(int i = 0; i < nums.size(); i ++){
            if(count1 < mid){
                count1 += res[i].second;

                if(count1 >= mid){
                    zhon = res[i].first;
                    break;
                }
            }
            
        }

        for(int i = 0; i < nums.size(); i ++){
            if(res[i].first != zhon)  ans += abs(zhon - res[i].first) * res[i].second;
        }

        return ans;
    }
};

 第四题icon-default.png?t=M85Bhttps://leetcode.cn/problems/minimum-number-of-operations-to-make-arrays-similar/description/

由于每一步操作的变化量的绝对值都是 2, 所以我们可以考虑分别把 nums 与 target 奇数元素及偶数元素分别归类进行求解,我们可以先对给定的两个数组进行排序, 进而归类后的数组也一定有序。因为一定有解所以两个数组的奇数个数与偶数个数一定相等。我们可以贪心把所有对应的位置上的差的绝对值再除以 2

class Solution {
public:
    long long makeSimilar(vector<int>& nums, vector<int>& target) {
        sort(nums.begin(), nums.end());
        sort(target.begin(), target.end());

        long long ans = 0;
        int ij[2]{};
        for(auto &c : nums){
            int p = c % 2;
            int &i = ij[p];

            while(target[i] % 2 != p)  i ++;

            ans += abs(c - target[i ++]);
        }

        return ans / 4;
    }
};

acwing : 

第一题icon-default.png?t=M85Bhttps://www.acwing.com/problem/content/4710/遍历一遍求出结果

#include <bits/stdc++.h>
using namespace std;

int main(){
    int n;
    cin >> n;
    vector<int> a(n);
    
    for(int i = 0; i < n; i ++){
        cin >> a[i];
    }
    
    int ans = abs(a[n - 1] - a[0]);
    for(int i = 1; i < n; i ++){
        ans = min(ans, abs(a[i] - a[i - 1]));
    }
    
    cout << ans;
    return 0;
}

第二题icon-default.png?t=M85Bhttps://www.acwing.com/problem/content/4711/三维的深度优先搜索,有六个偏移量,模板题

#include <bits/stdc++.h>
using namespace std;

char dist[11][11][11];
int count1 = 0;
int k, n, m;

void dfs(int t, int x, int y){
    if(t < 1 || t > k || x < 1 || x > n || y < 1 || y > m || dist[t][x][y] == '#')  return ;
    
    count1 ++;
    dist[t][x][y] = '#';
    
    dfs(t + 1, x, y);
    dfs(t - 1, x, y);
    dfs(t, x + 1, y);
    dfs(t, x - 1, y);
    dfs(t, x, y + 1);
    dfs(t, x, y - 1);
}

int main(){
    cin >> k >> n >> m;
    
    for(int c = 1; c <= k; c ++){
        for(int i = 1; i <= n; i ++){
            for(int j = 1; j <= m; j ++){
                cin >> dist[c][i][j];
            }
        }
    }
    
    int x, y, t = 1;
    cin >> x >> y;
    
    dfs(t, x, y);
    
    cout << count1 << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值