【2022/04/10-第288场单周赛】复盘

总结

第四题第一参照选错了导致咋也做不出来。

Q1.按奇偶性交换后的最大数字

对奇偶性相同的所有数做冒泡,把较大的放到前面位置。

class Solution {
public:


    int largestInteger(int num) {
        vector<int> nums, odd, even, h[10];
        while(num){
            nums.push_back(num % 10);
            num /= 10;
        }
        int n = nums.size();
        reverse(nums.begin(), nums.end());
        for(int i = 0; i < nums.size(); ++i){
            for(int j = i + 1; j < nums.size(); ++j){
                if(nums[i] % 2 == nums[j] % 2 && nums[i] < nums[j]) swap(nums[i], nums[j]);
            }
        }
        long long ret = 0;
        for(int i = 0; i < nums.size(); ++i){
            ret = ret * 10 + nums[i];
        }
        return ret;
    }
};

Q2.向表达式添加括号后的最小结果

拆分出两个数字,然后枚举左右括号位置。

class Solution {
public:
    string minimizeResult(string ex) {
        int pos;
        for(pos = 0; pos < ex.size(); ++pos) if(ex[pos] == '+') break;
        string ls = ex.substr(0, pos), rs = ex.substr(pos + 1, ex.size() - pos - 1);
        int mino = INT_MAX, resi, resj;
        for(int i = 0; i < ls.size(); ++i){
            for(int j = 1; j <= rs.size(); ++j){
                int lc, lj, rc, rj;
                if(i == 0) lc = 1;
                else lc = stoi(ls.substr(0, i));
                if(j == rs.size()) rc = 1;
                else rc = stoi(rs.substr(j, rs.size() - j));
                if(i == ls.size()) lj = 0;
                else lj = stoi(ls.substr(i, ls.size() - i));
                if(j == 0) rj = 0;
                else rj = stoi(rs.substr(0, j));
                if(i == ls.size() && j == 0) continue;
                if(lc * (lj + rj) * rc < mino){
                    mino = lc * (lj + rj) * rc;
                    resi = i;
                    resj = j;
                }
            }
        }
        ex.insert(ex.begin() + resi, '(');
        ex.insert(ex.begin() + pos + 2 + resj, ')');
        return ex;
    }
};

Q3.K 次增加后的最大乘积

哈夫曼树的思想,用小顶堆存储即可。

class Solution {
public:
    int maximumProduct(vector<int>& nums, int k) {
        priority_queue<int, vector<int>, greater<int>> q;
        for(auto i : nums) q.push(i);
        while(k--){
            int v = q.top();
            ++v;
            q.pop();
            q.push(v);
        }
        long long ret = 1;
        while(!q.empty()){
            ret = (ret * q.top()) % 1000000007;
            q.pop();
        }
        return ret;
    }
};

Q4.花园的最大总美丽值

挺恶心的。
先对数组排序,把已经美丽的剔除。然后枚举种成美丽的数目,然后对于剩下的花坛尽量使得最小值最大。

typedef long long ll;
class Solution {
public:
    long long maximumBeauty(vector<int>& flowers, long long newFlowers, int target, int full, int partial) {
        sort(flowers.begin(), flowers.end());
        
        ll n = flowers.size(), sum = 0, fcnt = 0, j = (int)flowers.size() - 1, T = target - 1;
        for(int i : flowers) {
            sum += i;
        }
        
        ll res = 0;
        
        /* flowers 为剩余需要成为 partial 花坛 */
        while(flowers.size() && newFlowers >= 0) {
            /* 数量超过 T 的花坛不需要再种花 */
            while(j >= 0 && flowers[j] > T) {
                sum -= flowers[j];
                j--;
            }
            
            if(j >= 0) {
                /* 双指针 */
                while(T * (j + 1) - sum > newFlowers) {
                    --T;
                    while(j >= 0 && (flowers[j] > T)) {
                        sum -= flowers[j];
                        --j;
                    }
                }
                res = max(res, T * partial + (n - (ll)flowers.size()) * full);
            }
            
            newFlowers -= max(0, target - flowers.back());
            if(j == (int)flowers.size() - 1) {
                sum -= flowers[j];
                j--;
            }
            flowers.pop_back();
        }
        
        /* 所有花坛都是 full */
        if(newFlowers >= 0) {
            res = max(res, n * full);
        }
        return res;
    }
};

posted on 2022-05-05 14:42  damnglamour  阅读(21)  评论(0)    收藏  举报