第 95 场双周赛

1.根据规则将箱子分类

根据规则将箱子分类

Solution

class Solution {
public:
    string categorizeBox(int length, int width, int height, int mass) {
        long long w = 1e4, v = 1e9;
        int ans[4];
        memset(ans,0,sizeof(ans));
        if(length >= w || width >= w || height >= w || (length * width) >= v / height){
            ans[0] = 1;
        }
        if(mass >= 100){
            ans[1] = 1;
        }
        if(ans[1] == 1 && ans[0] == 1){
            return "Both";
        }
        if(ans[0] != 1 && ans[1] != 1){
            return "Neither";
        }
        if(ans[1] == 1 && ans[0] != 1){
            return "Heavy";
        }
        return "Bulky";
    }
};

2.找到数据流中的连续整数

找到数据流中的连续整数

Solution

class DataStream {
public:
    int value, k;
    int cnt = 0;
    
    DataStream(int value, int k) {
        this->value = value;
        this->k = k;
    }
    
    bool consec(int num) {
        if (num == value) {
            cnt++;
        } else {
            cnt = 0;
        }
        return cnt >= k;
    }
};

/**
 * Your DataStream object will be instantiated and called as such:
 * DataStream* obj = new DataStream(value, k);
 * bool param_1 = obj->consec(num);
 */

3.查询数组 Xor 美丽值

查询数组 Xor 美丽值

Solution

class Solution {
public:
    int xorBeauty(vector<int>& nums) {
        return reduce(nums.begin(), nums.end(), 0, bit_xor<>{});
    }
};

4.最大化城市的最小供电站数目

最大化城市的最小供电站数目

Solution

class Solution {
public:
    long long maxPower(vector<int>& stations, int r, int k) {
        int n = stations.size();
        long sum[n + 1], power[n], diff[n];
        sum[0] = 0;
        //计算前缀和
        for(int i = 0;i < n;i++){
            sum[i + 1] = sum[i] + stations[i];
        }
        for(int i = 0;i < n;i++){
            power[i] = sum[min(i + r + 1, n)] - sum[max(i - r, 0)];//计算电量
        }
        auto check = [&](long min_power) -> bool{
            memset(diff, 0, sizeof(diff));
            long sum_d = 0, need = 0;
            for(int i = 0;i < n;i++){
                sum_d += diff[i];
                long m = min_power - power[i] - sum_d;
                if(m > 0){
                    need += m;
                    if(need > k) return false;
                    sum_d += m;
                    if (i + r * 2 + 1 < n) diff[i + r * 2 + 1] -= m;
                }
            }
            return true;
        };
        long left = *min_element(power, power + n), right = left + k + 1; 
        while(left + 1 < right){
            long mid = left + (right - left) / 2;
            check(mid) ? left = mid : right = mid;
        }
        return left;
    }
};
posted @ 2023-01-10 00:47  TTS-S  阅读(2)  评论(0编辑  收藏  举报