[LeetCode 1351~1354][周赛]周赛176题解

这场周赛还是有点意思的,没有那么水,大概hard有cf div2 D的感觉
1351. 统计有序矩阵中的负数
题目链接
第一种:二分

static const auto io_speed_up = [](){
    ios::sync_with_stdio(false);
    cin.tie(0);
    return 0;
}();
class Solution {
public:
    int countNegatives(vector<vector<int>>& grid) {
        int y = 0, ans = 0;
        for(auto &p : grid){
            y = p.end() - upper_bound(p.begin(),p.end()-y,0, greater<int>());
            ans += y;
        }
        return ans;
    }
};

第二种.倒序遍历

static const auto io_speed_up = [](){
    ios::sync_with_stdio(false);
    cin.tie(0);
    return 0;
}();
class Solution {
public:
    int countNegatives(vector<vector<int>>& grid) {
        int n = grid.size(), m = grid[0].size();
        int y = m - 1, ans = 0;
        for(auto &p : grid){
            while(y>=0&&p[y]<0)y--;
            ans += (m-1-y);
        }
        return ans;
    }
};

1352.最后 K 个数的乘积
题目链接
维护一下最后出现0的位置,然后把0当作1维护一下积的前缀

static const auto io_speed_up = [](){
    ios::sync_with_stdio(false);
    cin.tie(0);
    return 0;
}();
class ProductOfNumbers {
public:
    vector<int>ans;
    int tl = 0;
    ProductOfNumbers() {
        ans.emplace_back(1);
    }
    
    void add(int num) {
        if(!num){tl=0;ans.emplace_back(1);}
        else {tl++;ans.emplace_back(num*ans.back());}
    }
    
    int getProduct(int k) {
        return k>tl?0:ans.back()/ans[ans.size()-1-k];
    }
};

/**
 * Your ProductOfNumbers object will be instantiated and called as such:
 * ProductOfNumbers* obj = new ProductOfNumbers();
 * obj->add(num);
 * int param_2 = obj->getProduct(k);
 */

1353.最多可以参加的会议数目
题目链接
先按开始时间排个序,可以想到在一个时刻去参加结束时间最早的会议比较好。

static const auto io_speed_up = [](){
    ios::sync_with_stdio(false);
    cin.tie(0);
    return 0;
}();
bool cmp1(vector<int>& a,vector<int>& b){
    return a[0] < b[0];
}
class Solution {
public:
    int maxEvents(vector<vector<int>>& events) {
        sort(events.begin(),events.end(),cmp1);
        priority_queue<int,vector<int>,greater<int> >mp;
        int n = events.size();
        int ans = 0,st = events[0][0];
        int i = 0;
        while(i<n){
            while(events[i][0]>st&&!mp.empty()){
                int k = mp.top();mp.pop();
                if(k>=st){st++,ans++;}
            }
            if(mp.empty())st = max(st,events[i][0]);
            while(i<n&&events[i][0]<=st){
                mp.push(events[i][1]);
                i++;
            }
        }
        while(!mp.empty()){
            int k = mp.top();mp.pop();
            if(k>=st){st++,ans++;}
        }
        return ans;
    }
};

1354.多次求和构造目标数组
题目链接
首先容易想到这个过程是可逆的,只需要求一下和 s u m sum sum,然后把最大值 b b b替换为 b + ( b − s u m ) b+(b-sum) b+(bsum),最后检查数组是否合法似乎就能做了。但是直接这样做会因为最大值比第二大的值大太多从而收敛过慢超时。
那么可以考虑一步将最大值收敛到第二大:当 b b b一直为最大值时, b − s u m b-sum bsum不变,那么做 k k k次操作的结果是 b : → b − k ∗ ( b − s u m ) b:\rightarrow b-k*(b-sum) b:bk(bsum),把上式的k解出就可以快速收敛。

static const auto io_speed_up = [](){
    ios::sync_with_stdio(false);
    cin.tie(0);
    return 0;
}();
typedef long long LL;
class Solution {
public:
    bool isPossible(vector<int>& target) {
        priority_queue<int>mq(target.begin(),target.end());
        LL sum = 0, n = target.size();
        if(n == 1){
            if(target[0]==1)return 1;
            else return 0;
        }
        for(int p : target){sum+=p;}
        while(!mq.empty()){
            if(sum <=n || mq.top()<=1)break;
            int k = mq.top();mq.pop();
            LL nxt = mq.top();
            LL kk = (k - nxt - 1)/(sum - k);
            nxt = k - (kk + 1)*(sum - k);
            sum = k - (kk)*(sum - k);
            if(sum <=n || nxt < 1)break;
            mq.push(nxt);
        }
        if(sum == n && mq.top()==1)return 1;
        return 0;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值