題解/算法 {2542. 最大子序列的分数}

Link

Solution

Method-1:
All possible answers are in the form sum * B[i], we can iterate all B[i] increasingly (now, the corresponding B of all elements insum must ≥ B [ i ] \geq B[i] B[i]);
This is feasible, but complicated;

@Delimiter

The above method is based on iterating B[i] increasingly, we can do that decreasingly;
When we at B[i], let multiset S be all elements whose B[] are ≥ B [ i ] \geq B[i] B[i], we choose the Greatest-K elements of S S S;

Code

long long maxScore(vector<int>& A, vector<int>& B, int K) {
    int n = A.size();
    vector< int> ind( n);
    for( int i = 0; i < n; ++i) ind[ i] = i;
    sort( ind.begin(), ind.end(), [&]( int _a, int _b) -> bool{ return B[ _a] > B[ _b];});
    long long ans = 0;
    priority_queue< int, vector<int>, greater<int> > Heap;
    long long sum = 0;
    for( int i = 0; i < n; ++i){
        int ii = ind[ i];
        Heap.push( A[ ii]);
        sum += A[ ii];
        if( Heap.size() > K){
            sum -= Heap.top();
            Heap.pop();
        }
        if( Heap.size() == K){
            ans = max( sum * B[ ii], ans);
        }
    }
    return ans;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值